【BFS】CODE[VS] 1226 倒水問題 (BFS+模擬)

點擊被抓去倒水


這道題主要考察代碼能力
模擬倒水的過程,結果因爲碼力太弱,漏了兩種情況(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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章