标签:c. ice cave cf #301 div. 2 搜索bfs
思路:若终点vis>=2就表明可以。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 1005 #define MAXN 2005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; struct Node { int x,y; }; char mp[555][555]; int vis[555][555]; int dir[4][2]={1,0,-1,0,0,1,0,-1}; int n,m,sx,sy,tx,ty; bool isok(int x,int y) { if (x>=1&&x<=n&&y>=1&&y<=m) return true; return false; } bool bfs() { Node st,now; queue<Node>Q; st.x=sx,st.y=sy; vis[sx][sy]=1; Q.push(st); while (!Q.empty()) { st=Q.front(); Q.pop(); if (vis[tx][ty]>=2) return true; for (int i=0;i<4;i++) { now.x=st.x+dir[i][0]; now.y=st.y+dir[i][1]; if (isok(now.x,now.y)&&((mp[now.x][now.y]!='X'&&!vis[now.x][now.y])||(now.x==tx&&now.y==ty))) { Q.push(now); vis[now.x][now.y]++; } } } return false; } int main() { // freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin); int i,j; while (~scanf("%d%d",&n,&m)) { memset(vis,0,sizeof(vis)); for (i=1;i<=n;i++) { scanf("%s",mp[i]+1); for (j=1;j<=m;j++) if (mp[i][j]=='X') vis[i][j]++; } scanf("%d%d%d%d",&sx,&sy,&tx,&ty); if (bfs()) printf("YES\n"); else printf("NO\n"); } return 0; }
C. Ice Cave (CF #301 (Div. 2) 搜索bfs)
标签:c. ice cave cf #301 div. 2 搜索bfs
原文地址:http://blog.csdn.net/u014422052/article/details/45417773