【BFS】CODE[VS] 1226 倒水问题 (BFS+模拟)

来源:互联网 发布:优雅的女生知乎 编辑:程序博客网 时间:2024/06/03 09:27

点击被抓去倒水


这道题主要考察代码能力
模拟倒水的过程,结果因为码力太弱,漏了两种情况(x和y全倒入一个(x或y)不满)
然后就没有然后了

最近超喜欢压代码怎么破???


代码如下:

#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#include <queue>const int maxn = 333;using namespace std;int x,y,z;bool vis[maxn][maxn];struct state{    int x,y,step;};state f;queue<state>q;int bfs(){    q.push(f);    vis[f.x][f.y] = 1;    while(q.size())    {        f = q.front();        q.pop();        if(f.x == z|| f.y == z) return f.step;//最近超喜欢压代码!         if(f.x < x && vis[x][f.y] == 0) { q.push((state){x,f.y,f.step+1}); vis[x][f.y] = 1;}//倒满x         if(f.y < y && vis[f.x][y] == 0) { q.push((state){f.x,y,f.step+1}); vis[f.x][y] = 1;}//倒满y         if(f.x && vis[0][f.y] == 0) { q.push((state){0,f.y,f.step+1}); vis[0][f.y] = 1;}//倒空x         if(f.y && vis[f.x][0] == 0) { q.push((state){f.x,0,f.step+1}); vis[f.x][0] = 1;}//倒空y         if(f.x >= y-f.y && vis[f.x-(y-f.y)][y] == 0){ q.push((state) {f.x-(y-f.y),y,f.step+1}); vis[f.x-(y-f.y)][y] = 1;}//从x倒入y中填满y         if(f.y >= x-f.x && vis[x][f.y-(x-f.x)] == 0){ q.push((state) {x,f.y-(x-f.x),f.step+1}); vis[x][f.y-(x-f.x)] = 1;}//从y倒入x中填满x         if(f.x < y-f.y && vis[0][f.x+f.y] == 0) { q.push((state){0,f.x+f.y,f.step+1}); vis[0][f.x+f.y] = 1;}//全部x加入y            if(f.y < x-f.x && vis[f.x+f.y][0] == 0) { q.push((state){f.x+f.y,0,f.step+1}); vis[f.x+f.y][0] = 1;}//全部y加入x     }    return -1; }int main(){    scanf("%d%d%d",&x,&y,&z);    int ans = bfs();    if(ans != -1)    {        printf("%d\n",ans);        return 0;       }     printf("impossible\n");return 0;}

THE END

By Peacefuldoge

http://blog.csdn.net/loi_peacefuldog

0 0
原创粉丝点击