文章标题 CSU 1856: Sokoban(模拟)
来源:互联网 发布:淘宝导航制作 编辑:程序博客网 时间:2024/05/21 10:45
1856: Sokoban
这里写链接内容
题意:推箱子问题
分析: 找到w的位置;(w && W)
按照题目给的方案模拟;
代码:
#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<vector>#include<math.h>#include<map>#include<queue> #include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;char ch[20][20];int n,m;char s[55]; int check(int x,int y,char c){//在x,y,这个位置执行c命令 int flag=0;//标记能否成功移动 if (c=='U'){//如果是向上走 if (x-1>=1&&ch[x-1][y]=='.'||ch[x-1][y]=='+'){ ch[x-1][y]=ch[x-1][y]=='.'?'w':'W'; ch[x][y]=ch[x][y]=='W'?'+':'.'; flag=1; } else if (x-2>=1&&(ch[x-1][y]=='b'||ch[x-1][y]=='B')&&(ch[x-2][y]=='.'||ch[x-2][y]=='+')){ ch[x-2][y]=ch[x-2][y]=='+'?'B':'b'; ch[x-1][y]=ch[x-1][y]=='B'?'W':'w'; ch[x][y]=ch[x][y]=='W'?'+':'.'; flag=1; } //return 1; } else if (c=='D'){ if (x+1<=n&&ch[x+1][y]=='.'||ch[x+1][y]=='+'){ ch[x+1][y]=ch[x+1][y]=='.'?'w':'W'; ch[x][y]=ch[x][y]=='W'?'+':'.'; flag=1; } else if (x+2<=n&&(ch[x+1][y]=='b'||ch[x+1][y]=='B')&&(ch[x+2][y]=='.'||ch[x+2][y]=='+')){ ch[x+2][y]=ch[x+2][y]=='+'?'B':'b'; ch[x+1][y]=ch[x+1][y]=='B'?'W':'w'; ch[x][y]=ch[x][y]=='W'?'+':'.'; flag=1; } //return 1; } else if (c=='R'){ if (y+1<=m&&ch[x][y+1]=='.'||ch[x][y+1]=='+'){ ch[x][y+1]=ch[x][y+1]=='.'?'w':'W'; ch[x][y]=ch[x][y]=='W'?'+':'.'; flag=1; } else if (y+2<=m&&(ch[x][y+1]=='b'||ch[x][y+1]=='B')&&(ch[x][y+2]=='.'||ch[x][y+2]=='+')){ ch[x][y+2]=ch[x][y+2]=='+'?'B':'b'; ch[x][y+1]=ch[x][y+1]=='B'?'W':'w'; ch[x][y]=ch[x][y]=='W'?'+':'.'; flag=1; } //return 1; } else if (c=='L'){ if (y-1>=1&&ch[x][y-1]=='.'||ch[x][y-1]=='+'){ ch[x][y-1]=ch[x][y-1]=='.'?'w':'W'; ch[x][y]=ch[x][y]=='W'?'+':'.'; flag=1; } else if (y-2>=1&&(ch[x][y-1]=='b'||ch[x][y-1]=='B')&&(ch[x][y-2]=='.'||ch[x][y-2]=='+')){ ch[x][y-2]=ch[x][y-2]=='+'?'B':'b'; ch[x][y-1]=ch[x][y-1]=='B'?'W':'w'; ch[x][y]=ch[x][y]=='W'?'+':'.'; flag=1; } //return 1; } return flag;}int judge(){ int cnt1=0,cnt2=0; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if (ch[i][j]=='+')cnt1++; if (ch[i][j]=='b')cnt2++; } } if (cnt1||cnt2)return 0; return 1;}int main (){ int cnt=1; while (cin>>n>>m){ if (n==0&&m==0)break; for (int i=1;i<=n;i++){ cin>>ch[i]+1; } int x,y; for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if (ch[i][j]=='w'||ch[i][j]=='W'){ x=i;y=j;//记住一开始的位置 } } } cin>>s; int comp=0; int len=strlen(s); for (int i=0;i<len;i++){ int flag=check(x,y,s[i]);//在x,y,这个位置执行s[i]命令 // for (int ii=1;ii<=n;ii++){// for (int jj=1;jj<=m;jj++){// printf ("%c",ch[ii][jj]);// }// printf ("\n");// } if (flag){ if (s[i]=='U')x--; else if (s[i]=='D')x++; else if (s[i]=='R')y++; else if (s[i]=='L')y--; } if (judge()){ comp=1; break; } } if (comp){ printf ("Game %d: complete\n",cnt++); } else { printf ("Game %d: incomplete\n",cnt++); } for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ printf ("%c",ch[i][j]); } printf ("\n"); } } return 0;}
阅读全文
0 0
- 文章标题 CSU 1856: Sokoban(模拟)
- 文章标题CSU 1862: The Same Game(模拟)
- hdu 4213 Sokoban(模拟)
- 文章标题 CSU 1836 :Square(高精度)
- 文章标题 CSU 1829: Dungeon(BFS)
- 文章标题 CSU 1832: Jump(DP)
- 文章标题 CSU 1756 :Prime
- 文章标题 CSU 1830: FarAway
- 文章标题 CSU 1578 : 爬楼梯 (简单DP)
- 文章标题 CSU 1815 : Enterprising Escape(BFS--优先队列)
- 文章标题 CSU 1831: Found(矩阵快速幂)
- 文章标题 CSU 1838: Water Pump (单调栈)
- 文章标题 CSU 1843: Jumping monkey (状态压缩+dp)
- 文章标题 CSU 1913 (线段树+二分答案)
- 文章标题 CSU 1846: Assembly line(DP)
- 文章标题 CSU 1859: Gone Fishing(贪心)
- 文章标题poj 1132: Border(模拟)
- G(1856): Sokoban
- 常见的八种导致 APP 内存泄漏的问题
- 单链表排序(函数指针)
- 加密lob数据+加密lob数据的方式+加密的语法
- 如何启用 Apache 的 PHP-FPM 多实例
- 解释器vs编译器 && C,java,python编译过程对比
- 文章标题 CSU 1856: Sokoban(模拟)
- 微信支付
- C语言的起源
- 使用Dockerfile制作Docker镜像
- Android动画之Animator
- device_create、class_create、driver_register函数
- 设置Intellij idea和maven,支持lambda表达式
- 立方体模型
- sublime常用快捷键