http://codeforces.com/contest/540/problem/C
题意 给n*m的地图
只能走.不能走X,并且点.被走了之后会变成X,问能否从点1走到点2,并且要求最后点2要是X(为了掉到下一level)
因此直接bfs搜,每次走过 点 后把 点变成X ,最后判断能否到达点2 并且点2状态为 X即可
另一种是直接判断能不能到点2,然后如果点2本身是X则输yes,
如果是点的话,只需要看能否往外走一步然后退回来即可
即看四周有没一个合法的点,
合法的点首先要是【点】,其次没被访问过,如果被访问过 【访问的步数必须==到达点2的步数-1】
1:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
const double pi=acos(-1.0);
double eps=0.000001;
__int64 min(__int64 a,__int64 b)
{return a<b? a:b ;}
__int64 max(__int64 a,__int64 b)
{return a>b?a:b;}
int n,m;
char mp[505][505];
int vis[505][505];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node
{
int x,y;
int step;
node(){}
node(int a,int b,int c)
{x=a,y=b;step=c;}
};
queue<node> q;
node st,ed;
int main()
{
int i;
cin>>n>>m;
int x,y;
for (i=1;i<=n;i++)
{
scanf("%s",mp[i]+1);
}
scanf("%d%d",&st.x,&st.y);
scanf("%d%d",&ed.x,&ed.y);
int flag=0;
st.step=0;
q.push(st);
while(!q.empty())
{
node tp=q.front();
q.pop();
for (i=0;i<4;i++)
{
int x=tp.x+dx[i];
int y=tp.y+dy[i];
if (x<1||x>n||y<1||y>m) continue;
// if (vis[x][y])continue;
if (x==ed.x&&y==ed.y&&mp[x][y]=='X')
{
flag=tp.step+1;break;
}
if (mp[x][y]=='X')continue;
mp[x][y]='X';
// vis[x][y]=tp.step+1;
q.push(node(x,y,tp.step+1));
}
if (flag)break;
}
if(flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return 0;
}
2:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
const double pi=acos(-1.0);
double eps=0.000001;
__int64 min(__int64 a,__int64 b)
{return a<b? a:b ;}
__int64 max(__int64 a,__int64 b)
{return a>b?a:b;}
int n,m;
char mp[505][505];
int vis[505][505];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node
{
int x,y;
int step;
node(){}
node(int a,int b,int c)
{x=a,y=b;step=c;}
};
queue<node> q;
node st,ed;
int main()
{
int i;
cin>>n>>m;
int x,y;
for (i=1;i<=n;i++)
{
scanf("%s",mp[i]+1);
}
scanf("%d%d",&st.x,&st.y);
scanf("%d%d",&ed.x,&ed.y);
int flag=0;
st.step=0;
q.push(st);
int lastx,lasty;
while(!q.empty())
{
node tp=q.front();
q.pop();
for (i=0;i<4;i++)
{
int x=tp.x+dx[i];
int y=tp.y+dy[i];
if (x<1||x>n||y<1||y>m) continue;
if (vis[x][y])continue;
if (x==ed.x&&y==ed.y)
{
flag=tp.step+1;
lastx=tp.x,lasty=tp.y;break;
}
if (mp[x][y]=='X')continue;
vis[x][y]=tp.step+1;
q.push(node(x,y,tp.step+1));
}
if (flag)break;
}
if(flag)
{
if (mp[ed.x][ed.y]=='X') printf("YES\n");
else
{
int cun=0;
for (i=0;i<4;i++)
{
int x=ed.x+dx[i];
int y=ed.y+dy[i];
if (x<1||x>n||y<1||y>m) continue;
if (mp[x][y]=='X')continue;
if ((vis[x][y]&&vis[x][y]<flag-1))continue;
if (x==lastx&&y==lasty) continue;
cun++;
}
if (cun>=1) printf("YES\n");
else
printf("NO\n");
}
}
else
{
printf("NO\n");
}
return 0;
}