URAL.1033 Labyrinth (DFS)

URAL.1033 Labyrinth (DFS)

题意分析

WA了好几发,其实是个简单地DFS。意外发现这个俄国OJ,然后发现ACRUSH把这个OJ刷穿了。

代码总览

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#define INF 0x3f3f3f3f
#define nmax 35
#define MEM(x) memset(x,0,sizeof(x))
using namespace std;
int spx[] = {0,1,0,-1};
int spy[] = {1,0,-1,0};
char mp[35][35];
bool visit[35][35];
int n,ans;
void dfs(int x,int y)
{
   for(int i=0;i<4;i++){
       int nx = x + spx[i];
       int ny = y +spy[i];
       if(!visit[nx][ny] && mp[nx][ny]=='.'){
          visit[nx][ny] = true;
          dfs(nx,ny);
       }
       else if(mp[nx][ny]=='#')
          ans++;
   }
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!= EOF){
        ans = 0;
        memset(mp,'#',sizeof(mp));
        for(int i=1;i<=n;i++){
            scanf("%s",mp[i]+1);
            mp[i][n+1] = '#';

        }
        visit[1][1] = true;
        dfs(1,1);
        if(!visit[n][n]){
            visit[n][n] = true;
            dfs(n,n);
        }
        printf("%d\n",(ans-4)*9);
    }
    return 0;
}
posted @ 2017-04-30 17:25  pengwill  阅读(94)  评论(0编辑  收藏  举报