码迷,mamicode.com
首页 > 其他好文 > 详细

HW18-广搜

时间:2020-06-09 23:56:43      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:pre   tor   level   描述   first   second   cout   iss   标记   

A:Saving Tang Monk

总时间限制: 
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
来源
Guo Wei
 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5 //要杀蛇 ,最多五条蛇 
 6 int n, k;
 7 char a[102][102];
 8 bool visited[102][102][10][1<<6];
 9 struct node{
10     int x, y;
11     int step, jewel,snake;
12     node(int xx, int yy, int ss, int jj, int kk):x(xx),y(yy),step(ss),jewel(jj),snake(kk){}
13 };
14 bool operator<(const node & a, const node & b){
15     return a.step>b.step;
16 }
17 priority_queue<node>pq;
18 int dirx[4] = {0,0,1,-1};
19 int diry[4] = {1,-1,0,0};
20 
21 int main(){
22 //    freopen("data.in","r", stdin);
23 //    freopen("data1.out","w", stdout); 
24     while(1){
25         cin>>n>>k;
26         if(n == 0) return 0;
27         int i, j;
28         while(!pq.empty()) pq.pop();
29         memset(visited, 0, sizeof(visited));
30         memset(a,0,sizeof(a));
31         int snakenum = 0;
32         for(i = 1; i <= n; i++)
33             for(j = 1; j <= n; j++){
34                 cin>>a[i][j];
35                 if(a[i][j]==K){
36                     pq.push(node(i,j,0,0,0));
37                     visited[i][j][0][0] = true;
38                 }
39                 if(a[i][j]==S){
40                     a[i][j] = A+snakenum;
41                     snakenum++;
42                 }
43             }    
44         bool flag = false; //标记是否有解 
45         while(!pq.empty()){
46             node no = pq.top();
47             pq.pop();
48             if(a[no.x][no.y]==T&&no.jewel==k){
49                 cout<<no.step<<endl;
50                 flag = true;
51                 break; 
52             }
53             for(int i = 0; i < 4; i++){
54                 int nx = no.x + dirx[i];
55                 int ny = no.y + diry[i];
56                 int nj = no.jewel;
57                 int ns = no.snake;
58                 if((a[nx][ny]==.||a[nx][ny]==T||a[nx][ny]==K)&&!visited[nx][ny][nj][ns]){
59                     pq.push(node(nx,ny,no.step+1,nj,ns));
60                     visited[nx][ny][nj][ns] = true;
61                 }
62                 else if(a[nx][ny]>=A&&a[nx][ny]<=A+snakenum){
63                     int news = ns|(1<<(a[nx][ny]-A));
64                     if(ns==news&&!visited[nx][ny][nj][ns]){ //这个蛇已经杀过了 
65                         pq.push(node(nx,ny,no.step+1,nj,ns));
66                         visited[nx][ny][nj][ns] = true;
67                     }
68                     else if(ns!=news&&!visited[nx][ny][nj][news]){
69                         pq.push(node(nx,ny,no.step+2,nj,news));
70                         visited[nx][ny][nj][news] = true;
71                     }
72                 }
73                 else if(a[nx][ny]>=1&&a[nx][ny]<=9){
74                     if(nj == a[nx][ny]-0-1&&!visited[nx][ny][nj+1][ns]){
75                         pq.push(node(nx,ny,no.step+1,nj+1,ns));
76                         visited[nx][ny][nj+1][ns] = true;
77                     }
78                     else if(!visited[nx][ny][nj][ns]){
79                         pq.push(node(nx,ny,no.step+1,nj,ns));
80                         visited[nx][ny][nj][ns] = true;
81                     }
82                 }
83             }
84         }
85         if(!flag) cout<<"impossible"<<endl;
86     }
87     return 0;
88 }

备注:

我窒息了。第一次WA是因为没看到题目里说5条蛇,蛇杀死就是死了,所以也要考虑进状态。第二次到第N次WA就要命了,瞪眼瞅了俩小时都没看出来,最后被逼无奈只好对拍(最后附上对拍的代码),第一次还造错了数据(蛇的数量超过了5),我还没看出来,抓耳挠腮又浪费了大把时光。终于!造出了正确数字,我在图上手动走的时候发现原来是K的原因!!!我光想着.和T都可以走了,但忘了起点也是可以再经过的!!!!!我服了。要涨这个教训。

其实我这么写是罗嗦的了。可以统一在最后进行push和visited操作。也就是说只要不过界和不是“#”就可以走,这样的话也不会犯我这个没走K的错误了。

以下是造数据的代码:方便起见只造了6*6的数据。

#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<iostream>
#include<cstring>
using namespace std;
char a[8][8];
int main(){
    freopen("data.in","w",stdout);
    srand(time(NULL));
    memset(a,0,sizeof(a));
    int n = 6, k = 6;
    cout<<n<<" "<<k<<endl;
    int i, j;
    int kx = rand()%6+1;
    int ky = rand()%6+1;
    int tx = rand()%6+1, ty = rand()%6+1;
    while(kx==tx&&ky==ty){
         tx = rand()%6+1; ty = rand()%6+1;
    }
    a[kx][ky] = K; a[tx][ty] = T;
    int jewel = 1;
    while(jewel<7){
        int tx = rand()%6+1; ty = rand()%6+1;
        while(a[tx][ty]!=0){
             tx = rand()%6+1; ty = rand()%6+1;
        }
        a[tx][ty] = jewel+0;
        jewel++;
    }
    int ss = 0;
    for(i = 1; i <= n; i++){
        for(j = 1; j <= n; j++){
            if(a[i][j]==0){
                if(rand()%10>7) a[i][j] = #;
                else if(rand()%10<3&&ss<=5){
                    a[i][j] = S;
                    ss++;
                }
                else a[i][j] = .;
            }
        }
    }
    for(i = 1; i <= n; i++){
        for(j = 1; j <= n; j++){
            cout<<a[i][j];
        }
        cout<<endl;
    }
    cout<<"0 0"<<endl;
    return 0;
}

以下是对拍.bat文件:

@echo off
:loop
MakeData.exe
Saving.exe
test.exe
fc data1.out data2.out
if not errorlevel 1 goto loop
pause
:end

呜呜呜。

HW18-广搜

标签:pre   tor   level   描述   first   second   cout   iss   标记   

原文地址:https://www.cnblogs.com/fangziyuan/p/13081426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!