POJ - 3984+POJ - 3414(BFS+路径记录)

题目一 迷宫问题

题目链接:

http://poj.org/problem?id=3984

题目:

Description

定义一个二维数组:

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

题目思路:

主要就是要记录路径  用一个队列recording 记录路径 然后才把recording加入BFS的队列里

AC代码:

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
char pic[5][5];
int vis[5][5];
struct node
{
    int x,y;
};
struct recording//用来存路径
{
    queue <node> q;
};
queue <recording> qq;
 
recording bfs()
{
    while(!qq.empty())qq.pop();
    node b;
    b.x=0;
    b.y=0;
    vis[b.x][b.y]=1;
    recording bb;
    while(!bb.q.empty())bb.q.pop();
    bb.q.push(b);
    qq.push(bb);
    while(!qq.empty())
    {
        recording nownow = qq.front();
        qq.pop();
        node now = nownow.q.back();
        if(now.x==4&&now.y==4){return nownow;}
        for(int d=0 ; d<4 ; d++)
        {
            recording nextnext = nownow;
            node next = now;
            now.x+=dx[d];
            now.y+=dy[d];
            if(next.x<0 || next.y<0 || next.x>=5 || next.y>=5)continue;
            if(pic[next.x][next.y]=='1' && vis[next.x][next.y]==1)continue;
            if(next.x==4 && next.y==4) {nextnext.q.push(next);  return nextnext ;}
            if(pic[next.x][next.y]=='0' && vis[next.x][next.y]==0)
            {
                vis[next.x][next.y] = 1;
                nextnext.q.push(next);
                qq.push(nextnext);
            }
        }
    }
}
int main()
{
//    freopen("in.txt","r",stdin);
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
            {scanf("%c",&pic[i][j]);getchar();}
    }
    memset(vis,0,sizeof(vis));
    recording output = bfs();
    while(!output.q.empty())
    {
        node out = output.q.front();
        output.q.pop();
        printf("(%d, %d)\n",out.x,out.y);
    }
    return 0;
}

题目二 Pots

题目链接:

http://poj.org/problem?id=3414

题目:

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

    1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
    2. DROP(i)      empty the pot i to the drain;
    3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

AC代码:

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
 
int vis[105][105],c,amax,bmax,flag;
struct node
{
    int a,b,step;
    string s;
};
struct recording
{
    queue <node> q;
};
queue <recording> qq;
 
recording bfs()
{
   while(!qq.empty())qq.pop();
   node be;
   be.a=0;
   be.b=0;
   be.step=0;
   be.s="";
   vis[be.a][be.b]=1;
   recording bb;
   bb.q.push(be);
   qq.push(bb);
   while(!qq.empty())
   {
        recording nownow = qq.front();
        node now = nownow.q.back();
        qq.pop();
        if(now.a==c || now.b==c){return nownow;}
        for(int d=0 ; d<6 ; d++)
        {
            recording nextnext = nownow;
            node next = now;
            if(d==0)//将第一个杯子倒满水
            {
                next.a = amax;
                if(vis[next.a][next.b]==1)continue;
                vis[next.a][next.b]=1;
                next.s="FILL(1)";
                next.step++;
                nextnext.q.push(next);
                qq.push(nextnext);
            }
            else if(d==1)//将第二个杯子倒满水
            {
                next.b = bmax;
                if(vis[next.a][next.b]==1)continue;
                vis[next.a][next.b]=1;
                next.s="FILL(2)";
                next.step++;
                nextnext.q.push(next);
                qq.push(nextnext);
            }
            else if(d==2)
            {
                next.a = 0;
                if(vis[next.a][next.b]==1)continue;
                vis[next.a][next.b]=1;
                next.s="DROP(1)";
                next.step++;
                nextnext.q.push(next);
                qq.push(nextnext);
            }
            else if(d==3)
            {
                next.b = 0;
                if(vis[next.a][next.b]==1)continue;
                vis[next.a][next.b]=1;
                next.s="DROP(2)";
                next.step++;
                nextnext.q.push(next);
                qq.push(nextnext);
            }
            else if(d==4)
            {
                if((bmax - next.b)>=next.a){next.b += next.a; next.a = 0;}
                else {next.a -= (bmax - next.b);  next.b = bmax; }
                if(vis[next.a][next.b]==1)continue;
                vis[next.a][next.b]=1;
                next.s="POUR(1,2)";
                next.step++;
                nextnext.q.push(next);
                qq.push(nextnext);
            }
            else if(d==5)
            {
                if((amax - next.a)>=next.b){next.a += next.b; next.b = 0;}
                else {next.b -= (amax - next.a);  next.a = amax; }
                if(vis[next.a][next.b]==1)continue;
                vis[next.a][next.b]=1;
                next.s="POUR(2,1)";
                next.step++;
                nextnext.q.push(next);
                qq.push(nextnext);
            }
            if(next.a == c || next.b == c)
                return nextnext;
        }
   }
   flag=1;
   return bb;
}
int main()
{
//    freopen("in.txt","r",stdin);
    while(scanf("%d %d %d",&amax,&bmax,&c)!=EOF)
    {
        if(amax==0 && bmax==0 && c==0)
            printf("impossible\n");
        else
        {
            memset(vis,0,sizeof(vis));
            flag=0;
            recording output = bfs();
            if(flag==0)
            {
                output.q.pop();
                node out = output.q.back();
                printf("%d\n",out.step);
                while(!output.q.empty())
                {
                    node out = output.q.front();
                    output.q.pop();
                    cout<<out.s<<endl;
                }
            }
            else printf("impossible\n");
        }
    }
    return 0;
}

 

posted @ 2018-08-30 19:28  PIPIXI  阅读(130)  评论(0编辑  收藏  举报
xs" style="position: fixed; opacity: 0.5; bottom: 300px; display: none">