累死了累死了,好多边界条件,一定要想清楚再写,后面修修补补,有很多地方应该都可以删掉的。
好在数据量很小,直接dfs即可
#include <iostream>
#include <climits>
using namespace std;
bool map[20][20]; // true表示能走
bool vis[20][20]; // 某个点有没有被访问过
int resMinLeft;
int resCnt;
void dfs(int x,int y,int &minLeft,int &curLeft,int &cnt,int direction){ // 以点(x,y)为出发点,direction:1->上,2->下,3->左,4->右
vis[x][y] = true; // 标记该点为已访问
curLeft--; // 把这个位置的果子吃掉惹
// 遍历上下左右,如果没有障碍物且没有被访问过,就从该点继续dfs
bool conti = false; // 是否能继续走
bool obsta = false; // 是否遇到障碍物
switch(direction){
case 1:// 往上走
if(vis[x - 1][y] == false && map[x - 1][y] == true){
conti = true; dfs(x - 1,y,minLeft,curLeft,cnt,direction);
}
break;
case 2: // 往下走
if(vis[x + 1][y] == false && map[x + 1][y] == true){
conti = true; dfs(x + 1,y,minLeft,curLeft,cnt,direction);
}
break;
case 3: // 往左走
if(vis[x][y - 1] == false && map[x][y - 1] == true){
conti = true; dfs(x,y - 1,minLeft,curLeft,cnt,direction);
}
break;
case 4: // 往右走
if(vis[x][y + 1] == false && map[x][y + 1] == true){
conti = true; dfs(x,y + 1,minLeft,curLeft,cnt,direction);
}
break;
}
if(conti == false){ // 直走的时候遇到障碍物了,尝试其他方向
if(vis[x - 1][y] == false && map[x - 1][y] == true){
conti = true;
dfs(x - 1,y,minLeft,curLeft,cnt,1);
}
if(vis[x + 1][y] == false && map[x + 1][y] == true){
conti = true;
dfs(x + 1,y,minLeft,curLeft,cnt,2);
}
if(vis[x][y - 1] == false && map[x][y - 1] == true){
conti = true;
dfs(x,y - 1,minLeft,curLeft,cnt,3);
}
if(vis[x][y + 1] == false && map[x][y + 1] == true){
conti = true;
dfs(x,y + 1,minLeft,curLeft,cnt,4);
}
}
if(conti == false){ // 到头了
if(curLeft < minLeft)
minLeft = curLeft;
}
vis[x][y] = false;
curLeft++;
// 退出之前将状态信息恢复
}
void process(int n,int k,int &res,int &curLeft,int &cnt){
int Left;
for(int i = 2;i <= n - 1;i++){
for(int j = 2;j <= n - 1;j++){
/*for(int t = 1;t <= n;t++){
fill(vis[t],vis[t] + n + 2,false);
}*/
int minLeft = INT_MAX;
Left = INT_MAX; // 找到以这个点为起点的最小剩余果子树
curLeft = (n - 2) * (n - 2) - k;
if(map[i][j] == true && map[i - 1][j] == true) dfs(i,j,minLeft,curLeft,cnt,1);
if(minLeft < Left) Left = minLeft;
if(map[i][j] == true && map[i + 1][j] == true) dfs(i,j,minLeft,curLeft,cnt,2);
if(minLeft < Left) Left = minLeft;
if(map[i][j] == true && map[i][j - 1] == true) dfs(i,j,minLeft,curLeft,cnt,3);
if(minLeft < Left) Left = minLeft;
if(map[i][j] == true && map[i][j + 1] == true) dfs(i,j,minLeft,curLeft,cnt,4);
if(minLeft < Left) Left = minLeft;
if(Left < res){
res = Left; cnt = 1;
}else if(Left == res) cnt++;
}
}
}
int main(){
int n,k; // 迷宫大小n * n,多余障碍数量k个,由题意可知坐标范围在1~n
scanf("%d%d",&n,&k);
for(int i = 0;i < 20;i++){
fill(map[i],map[i] + 20,true);
}
for(int i = 1;i <= n;i++){
if(i == 1 || i == n) fill(map[i],map[i] + n + 2,false);
else map[i][1] = false,map[i][n] = false;
}
for(int i = 0;i < k;i++){ // 每个障碍的位置
int x,y;
scanf("%d%d",&x,&y);
map[x][y] = false;
}
int res = INT_MAX;
int curLeft;
int cnt = 0;
process(n,k,res,curLeft,cnt);
resMinLeft = res,resCnt = cnt;
printf("%d %d\n",resMinLeft,resCnt);
bool opti = false;
int rescnt = 0;
for(int i = 2;i <= n - 1;i++){
for(int j = 2;j <= n - 1;j++){
if(map[i][j] == true){
map[i][j] = false; // 在这里设置障碍
res = INT_MAX;
cnt = 0;
process(n,k + 1,res,curLeft,cnt);
if(res < resMinLeft){
resMinLeft = res;
rescnt = 1;
opti = true;
}else if(res == resMinLeft) rescnt++;
map[i][j] = true; // 恢复状态信息
}
}
}resCnt = rescnt;
if(opti == false) printf("-1");
else printf("%d %d",resMinLeft,resCnt);
system("pause");
return 0;
}