题意:已知迷宫形态,确定墙的数目。
思路:基本DFS
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
#define MAXN 35
int n,sum;
char map[MAXN][MAXN];
bool visit[MAXN][MAXN];
int way[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
bool check(int x,int y){
return x>=0&&x<n&&y>=0&&y<n;
}
void dfs(int x,int y){
visit[x][y]=1;
int a,b;
for(int i=0;i<4;i++){
a=x+way[i][0];
b=y+way[i][1];
if(!check(a,b)||map[a][b]=='#') sum++;
else if(check(a,b)&&!visit[a][b]&&map[a][b]!='#') dfs(a,b);
}
}
int main(){
while(cin>>n){
for(int i=0;i<n;i++){
cin>>map[i];
}
sum=0;
memset(visit,0,sizeof(visit));
dfs(0,0);
if(!visit[n-1][n-1]) dfs(n-1,n-1);
cout<<(sum-4)*9<<endl;
}
return 0;
}
/*测试:
Sample Input
5
.....
...##
..#..
..###
.....
Sample Output
198
*/