Kaitou Kid

来源:互联网 发布:阿芙护肤品怎么样知乎 编辑:程序博客网 时间:2024/05/16 01:40

Kaitou Kid - The Phantom Thief (2)
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 234 Accepted Submission(s): 99

Problem Description
破解字迷之后,你得知Kid将会在展览开始后T分钟内盗取至少一颗宝石,并离开展馆。整个展馆呈矩形分布,划分为N*M个区域,有唯一的入口和出口(不能从出口进入,同样不能从入口出去)。由某个区域可直接移动至相邻四个区域中的一个,且最快需要一分钟。假设Kid进入放有宝石的区域即可盗取宝石,无需耗时。问至少要封锁几个区域(可以封锁放有宝石的区域,但不能封锁入口和出口)才能保证Kid无法完成任务。

Input
输入的第一行有一个整数C,代表有C组测试数据。每组测试数据的第一行有三个整数N,M,T(2<=N,M<=8,T>0)。接下来N行M列为展馆布置图,其中包括:

‘S’:入口
‘E’:出口
‘J’:放有宝石的区域,至少出现一次
‘.’:空白区域
‘#’:墙

Output

        对每组测试数据,输出至少要封锁的区域数。

Sample Input
2
5 5 5
SJJJJ
..##J
.JJJJ
.J…
EJ…
5 5 6
SJJJJ
..##J
.JJJJ
.J…
EJ…

Sample Output
0
2
/*
解题思路 本体就是bfs加dfs 其中dfs最多点用4层 因为封锁点最多封锁4个就可以了 然后只得注意的是 visit数组 我们这里用一个三维数组来做 第三位数组表示是否有珠宝从中经过 然后结构体 多加了一个flag 表示是否有宝石
*/

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<string>#include<queue>using namespace std;typedef struct LNode{    int x;    int y;    int step;    int flag;}node;int visit[10][10][2];char mp[11][11];int dir[4][2] = {0,-1,1,0,0,1,-1,0};int sx,sy,ex,ey;int h,w,t;int ans,val,minstep;bool bfs(){    queue <LNode> q;    node now;    now.x = sx;    now.y = sy;    now.step = 0;    now.flag = 0;    memset(visit,0,sizeof(visit));    visit[sx][sy][0] = 1;    q.push(now);    while(!q.empty())    {        now = q.front();        q.pop();        if(now.step <= t)        if(ex==now.x&&ey==now.y&&visit[now.x][now.y][1]) return false;     //   if(now.step>t) continue;        for(int i =0;i<4;i++){            int xx = now.x + dir[i][0];            int yy = now.y + dir[i][1];            if(xx>=0&&yy>=0&&xx<h&&yy<w&&mp[xx][yy]!='#'){                node nxt;                if(mp[xx][yy]=='J') nxt.flag = 1;                else nxt.flag = now.flag;                if(!visit[xx][yy][nxt.flag]){                    nxt.x = xx;                    nxt.y = yy;                    nxt.step = now.step +1;                    visit[xx][yy][nxt.flag] = 1;                    q.push(nxt);                }            }        }    }    return true;}bool dfs(int deep){    if(!deep){        bool b = bfs();        /*if(b){            for(int i =0;i<h;i++){                for(int j =0;j<w;j++)                    cout<<mp[i][j];                cout<<endl;            }        }*/        return b;    }    for(int i =0;i<h;i++){        for(int j = 0;j<w;j++){            char ch = mp[i][j];            if(ch=='#'||ch=='S'||ch=='E')continue;            mp[i][j] = '#';          //  cout <<" "<<i<<" "<<j<<endl;            if(dfs(deep-1)) return true;            mp[i][j] = ch;        }    }    return false;}int main(){    int ncase;    //freopen("in.txt","r",stdin);    cin>>ncase;    while(ncase--)    {        cin>>h>>w>>t;       // ans = 0;        for(int i =0;i<h;i++){            scanf("%s",mp[i]);            for(int j =0;j<strlen(mp[i]);j++){                if(mp[i][j]=='S')                {                    sx = i;                    sy = j;                }else if(mp[i][j]=='E')                {                    ex = i;                    ey = j;                }            }        }        if(bfs())        {            cout<<0<<endl;            continue;        }        int i ;        for(i = 1;i<4;i++){            if(dfs(i))            {                break;            }        }        cout<<i<<endl;    }    return 0;}