4130:Saving Tang Monk(bfs)

 

总时间限制: 
1000ms
 
内存限制: 
65536kB
描述

《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts.

During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.

Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace. But to rescue Tang Monk, Sun Wukong might need to get some keys and kill some snakes in his way.

The palace can be described as a matrix of characters. Each character stands for a room. In the matrix, 'K' represents the original position of Sun Wukong, 'T' represents the location of Tang Monk and 'S' stands for a room with a snake in it. Please note that there are only one 'K' and one 'T', and at most five snakes in the palace. And, '.' means a clear room as well '#' means a deadly room which Sun Wukong couldn't get in.

There may be some keys of different kinds scattered in the rooms, but there is at most one key in one room. There are at most 9 kinds of keys. A room with a key in it is represented by a digit(from '1' to '9'). For example, '1' means a room with a first kind key, '2' means a room with a second kind key, '3' means a room with a third kind key... etc. To save Tang Monk, Sun Wukong must get ALL kinds of keys(in other words, at least one key for each kind).

For each step, Sun Wukong could move to the adjacent rooms(except deadly rooms) in 4 directions(north,west,south and east), and each step took him one minute. If he entered a room in which a living snake stayed, he must kill the snake. Killing a snake also took one minute. If Sun Wukong entered a room where there is a key of kind N, Sun would get that key if and only if he had already got keys of kind 1,kind 2 ... and kind N-1. In other words, Sun Wukong must get a key of kind N before he could get a key of kind N+1 (N>=1). If Sun Wukong got all keys he needed and entered the room in which Tang Monk was cuffed, the rescue mission is completed. If Sun Wukong didn't get enough keys, he still could pass through Tang Monk's room. Since Sun Wukong was a impatient monkey, he wanted to save Tang Monk as quickly as possible. Please figure out the minimum time Sun Wukong needed to rescue Tang Monk.

 

 

 

 

输入
There are several test cases.

For each case, the first line includes two integers N and M(0 < N <= 100, 0 < M <= 9), meaning that the palace is a N * N matrix and Sun Wukong needed M kinds of keys(kind 1, kind 2, ... kind M).

Then the N*N matrix follows.

The input ends with N = 0 and M = 0.
输出
For each test case, print the minimum time (in minute) Sun Wokong needed to save Tang Monk. If it's impossible for Sun Wokong to complete the mission, print "impossible".
样例输入
3 1
K.S
##1
1#T
3 1
K#T
.S#
1#.
3 2
K#T
.S.
21.
0 0
样例输出
5
impossible
8


和书上的有点区别。这个S被杀掉,书上的S没有杀掉。
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

struct node
{
    int x, y;
    int step;//表示的是花费时间 
    int key;//表示拥有的钥匙 
    bool snack[6];//表示的是访问节点的次数 
    friend bool operator < (node a, node b)//当返回为true的时候,说明B优先 
    {
        if(a.step!=b.step)
            return a.step>b.step;//谁步数小谁优先,步数相同大key优先 
        return a.key<b.key;
    }
}start, over;
 
int n, m;//m个钥匙 n*n
char st[105][105];
bool used[105][105][10];
int dx[4]={-1, 0, 1, 0};
int dy[4]={0, -1, 0, 1};
 
bool in(node a)
{
    return a.x>=1 && a.x<=n && a.y>=1 && a.y<=n;
}
 
int bfs()
{
    priority_queue <node> q;//小顶堆 。重载运算符<决定是小顶堆 
    while(!q.empty()) q.pop();
    start.step=start.key=0;
    memset(start.snack, 0, sizeof(start.snack));
    q.push(start);
    node a, b;
    while(!q.empty())
    {
        a=q.top();
        q.pop();
        if(a.x==over.x && a.y==over.y && a.key==n)
            return a.step;
        for(int i=0; i<4; i++)
        {
            b.x=a.x+dx[i];
            b.y=a.y+dy[i];
            b.key=a.key;
            if(in(b) && st[b.x][b.y]!='#' && !used[b.x][b.y][b.key])
            {
                for(int j=0; j<6; j++) b.snack[j]=a.snack[j];
                b.step=a.step+1;
                used[b.x][b.y][b.key]=1;
                if(b.x==over.x && b.y==over.y && b.key==m)
                    return b.step;
                else
                {
                    if(st[b.x][b.y]>0 && st[b.x][b.y]<6 && !b.snack[st[b.x][b.y]])
                    {
                        b.snack[st[b.x][b.y]]=1;
                        b.step++;
                    }
                    else if(st[b.x][b.y]-'0'==a.key+1)
                    {
                        b.key++;
                    }
                    q.push(b);
                }
            }
        }
    }
    return -1;
}
 
int main()
{
    int num;
    while(cin>>n>>m && n+m)
    {
        num=1;
        memset(used, 0, sizeof(used));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                cin>>st[i][j];
                if(st[i][j]=='K')
                {
                    start.x=i, start.y=j;
                    used[i][j][0]=1;
                }
                else if(st[i][j]=='T')
                {
                    over.x=i, over.y=j;
                }
                else if(st[i][j]=='S')
                {
                    st[i][j]=num;
                    num++;
                }
            }
        }
        int ans=bfs();
        if(ans==-1) cout<<"impossible"<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}

有机会再回头看自己的代码,哪里错了。贴上错的

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n,m;
 4 char a[105][105];
 5 int visit[105][105][10];
 6 struct Node {
 7     int r,c,step,k;
 8     Node(int rr,int cc,int ss,int kk):r(rr),c(cc),step(ss),k(kk) {}
 9     Node() {}
10 };
11 int dr[4]= {1,-1,0,0};
12 int dc[4]= {0,0,1,-1};
13 Node start,goal;
14 queue <Node> q;
15 int main() {
16     while(cin>>n>>m&&n!=0) {
17         memset(a,'#',sizeof(a));
18         memset(visit,0,sizeof(visit));
19         for(int i=1; i<=n; i++) {
20             for(int j=1; j<=n; j++) {
21                 cin>>a[i][j];
22                 if(a[i][j]=='K') {
23                     start=Node(i,j,0,0);
24                     visit[i][j][0]=1;
25                 }
26                 if(a[i][j]=='T') {
27                     goal=Node(i,j,-1,-1);
28 
29                 }
30             }
31         }
32         while(!q.empty())q.pop();
33         q.push(start);
34         while(!q.empty()) {
35             Node p=q.front();
36             q.pop();
37             if(p.r==goal.r&&p.c==goal.c&&p.k==m) {
38                 cout<<p.step<<endl;
39                 goto hhh;
40             }
41             if(a[p.r][p.c]=='S') {
42                 a[p.r][p.c]='.';
43                 q.push(Node(p.r,p.c,p.step+1,p.k));
44             } 
45             else {
46                 for(int i=0; i<4; i++) {
47                     int rr=p.r+dr[i];
48                     int cc=p.c+dc[i];
49                     if(a[rr][cc]=='#')continue;
50                     else if(!visit[rr][cc][p.k]&&a[rr][cc]==char('0'+p.k+1)) {
51                         visit[rr][cc][p.k+1]=1;
52                         q.push(Node(rr,cc,p.step+1,p.k+1));
53                     }
54 //                     else if(!visit[rr][cc][p.k]&&a[rr][cc]=='S') {
55 //                        a[rr][cc]='.';
56 //                        q.push(Node(p.r,p.c,p.step+1,p.k));
57 //                    } 
58                     else if(!visit[rr][cc][p.k]) {
59                         visit[rr][cc][p.k]=1;
60                         q.push(Node(rr,cc,p.step+1,p.k)) ;
61                     }
62                 }
63             }
64         }
65         cout<<"impossible"<<endl;
66 hhh:
67         ;
68     }
69 
70     return 0;
71 }

 

 
posted @ 2020-04-05 11:24  瓜瓜爱呱呱  阅读(637)  评论(0编辑  收藏  举报