http://codeforces.com/problemset/problem/128/A
题意:
给8x8棋盘,右上角是A,左下角是M
其余是点.或者是S
A不动,M每次可以朝着8个方向走,或不动。。。
每次A走一步后,所有的S都会往下移动一格(直到掉出棋盘)
问你M能否到达A,能输出win否则输出yes(走的过程如果碰到S会死,只能走空点)
开始时M先走
。。。。只有8X8嘛,显然8步之后,所有S都掉出棋盘了啊,只需要让M跑八步还不死就得了。。。
然后是每次S往下掉一格,也就是地图会变化
因为我们只需要枚举前7步,我们把0-7步的地图都预处理出来,
然后跑个bfs,看M能否在走了7步之后仍活下来(没碰到S)
。。。
卡了半天。。。原来是判断停在原地的情况时,少了一个if,也就是 if (格子==‘M’)也OK的情况.......跪啊,这么sb的错误。。。。。。。每次写搜索都犯sb错误
#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; char mp[20][20]; char ans[15][20][20]; struct node { int x,y; int step; node(int a=0,int b=0) {x=a,y=b;step=0;} node(int a,int b,int c) {x=a,y=b;step=c;} }; node ss[8*8+10]; int ok=0; void change() { int i; for (i=ok;i>=1;i--) { int x=ss[i].x; int y=ss[i].y; if (x>8)continue; mp[x][y]='.'; ss[i].x=x+1; if (x+1<=8) { mp[x+1][y]='S'; } } } queue<node >sb; int dx[]={0,0,-1,-1,-1,1,1,1,0}; int dy[]={1,-1,0,1,-1,0,1,-1,0}; int vis[20][20]; int main() { int n; int i,j,k; for (i=1;i<=8;i++) scanf("%s",mp[i]+1); for (i=1;i<=8;i++) { for (j=1;j<=8;j++) { if (mp[i][j]=='S') ss[++ok]=node(i,j); } } for (k=0;k<=9;k++) { for (i=1;i<=8;i++) { for (j=1;j<=8;j++) { ans[k][i][j]=mp[i][j]; } } change(); } int flag=0; sb.push(node(8,1)); while(!sb.empty()) { node tt=sb.front(); sb.pop(); if (ans[tt.step][tt.x][tt.y]=='S')continue; if (tt.step>=7) {flag=1;break;} for (i=0;i<9;i++) { int xx=tt.x+dx[i]; int yy=tt.y+dy[i]; if (xx>=1&&xx<=8&&yy>=1&&yy<=8) // if (vis[xx][yy]==0) if (ans[tt.step][xx][yy]=='.'||ans[tt.step][xx][yy]=='M') { sb.push(node(xx,yy,tt.step+1)); // vis[xx][yy]=1; } } } if (flag) printf("WIN\n"); else printf("LOSE\n"); return 0; }
#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 max(__int64 a,__int64 b) {return a>b?a:b;} int dx[]={0,0,0,1,1,1,-1,-1,-1}; int dy[]={0,1,-1,0,1,-1,0,1,-1}; char tm[10][10]; char after[10][10][10]; void change() { int j,k; for (j=8;j>=1;j--) { for (k=1;k<=8;k++) { if (tm[j][k]=='S') { tm[j][k]='.'; if (j!=8) tm[j+1][k]='S'; } } } } struct node { int x,y,step; node(){} node(int a,int b,int c=0){x=a,y=b;step=c;} }; int vis[10][10][10]; queue<node> q; int main() { int i,j,k; for (i=1;i<=8;i++) scanf("%s",tm[i]+1); for (j=1;j<=8;j++) { for (k=1;k<=8;k++) after[0][j][k]=tm[j][k]; } for (i=1;i<=7;i++) { change(); for (j=1;j<=8;j++) { for (k=1;k<=8;k++) after[i][j][k]=tm[j][k]; } } node st(8,1,0); q.push(st); int step=1; int flag=0; while(!q.empty()) { node tt=q.front(); q.pop(); if (after[tt.step][tt.x][tt.y]=='S') continue; if (tt.step==8) { flag=1; break; } for (i=0;i<9;i++) { int tx=tt.x+dx[i]; int ty=tt.y+dy[i]; if (!(tx>=1&&tx<=8&&ty>=1&&ty<=8))continue; if (after[tt.step][tx][ty]=='S') continue; if (vis[tt.step+1][tx][ty])continue; vis[tt.step+1][tx][ty]=1; q.push(node(tx,ty,tt.step+1)); } } if (flag) printf("WIN\n"); else printf("LOSE\n"); return 0; }