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

whust #0.2 J Jailbreak (未AC)

时间:2015-08-09 22:11:22      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

J
19
Jailbreak
John is on a mission to get two people out of prison. This particular prison is a one-story
building. He has managed to get hold of a detailed floor plan, indicating all the walls and
doors. He also knows the locations of the two people he needs to set free. The prison guards
are not the problem – he has planned a diversion that should leave the building practically
void.
The doors are his main concern. All doors are normally opened remotely from a control
room, but John can open them by other means. Once he has managed to open a door, it
remains open. However, opening a door takes time, which he does not have much of, since
his diversion will only work for so long. He therefore wants to minimize the number of doors
he needs to open. Can you help him plan the optimal route to get to the two prisoners?
Input
On the first line one positive number: the number of test cases, at most 100. After that per test
case:
• one line with two space-separated integers h and w (2 ≤ h, w ≤ 100): the width and
height of the map.
• h lines with w characters describing the prison building:




‘.’ is an empty space.
‘ * ’ is an impenetrable wall.
‘#’ is a door.
‘$’ is one of the two people to be liberated.
John can freely move around the outside of the building. There are exactly two people on the
map. For each person, a path from the outside to that person is guaranteed to exist.
Output
Per test case:
• one line with a single integer: the minimum number of doors John needs to open in
order to get to both prisoners.20
Problem J: Jailbreak
Sample in- and output
Input Output
3
5 9
****#****
*..#.#..*
****.****
*$#.#.#$*
*********
5 11
*#*********
*$*...*...*
*$*.*.*.*.*
*...*...*.*
*********.*
9 9
*#**#**#*
*#**#**#*
*#**#**#*
*#**.**#*
*#*#.#*#*
*$##*##$*
*#*****#*
*.#.#.#.*
********* 4
0
9

 

我的思路是这样的

先从两个人质出发做两遍bfs,做bfs的时候要记录路径

由于出口最多有400个(不到400)

然后枚举两个人质都能到达的出口

从两个人质到达该出口有两条路径

然后标记这两条路径上的门(注意重复的只标记一次)

然后统计门的数量.

然而wa了....

并不明白为什么.

改日再说.

技术分享
  1 /*************************************************************************
  2     > File Name: code/whust/#0.2/J.cpp
  3     > Author: 111qqz
  4     > Email: rkz2013@126.com 
  5     > Created Time: 2015年08月09日 星期日 12时54分56秒
  6  ************************************************************************/
  7 #include<iostream>
  8 #include<iomanip>
  9 #include<cstdio>
 10 #include<algorithm>
 11 #include<cmath>
 12 #include<cstring>
 13 #include<string>
 14 #include<map>
 15 #include<set>
 16 #include<queue>
 17 #include<vector>
 18 #include<stack>
 19 #define y0 abc111qqz
 20 #define y1 hust111qqz
 21 #define yn hez111qqz
 22 #define j1 cute111qqz
 23 #define tm crazy111qqz
 24 #define lr dying111qqz
 25 using namespace std;
 26 #define REP(i, n) for (int i=0;i<int(n);++i)  
 27 typedef long long LL;
 28 typedef unsigned long long ULL;
 29 const int inf = 0x7fffffff;
 30 const int N=1E2+5;
 31 char maze[N][N];
 32 int tar[5][2];
 33 int beg[4*N][2];
 34 int dx[4]={0,0,-1,1};
 35 int dy[4]={-1,1,0,0};
 36 int total;
 37 int h,w;
 38 int sum;
 39 bool v[N][N];
 40 struct NODE
 41 {
 42     int d,prex,prey;
 43 }q[N][N][2];
 44 bool ok ( int x,int y,int num)
 45 {
 46     if (x>=0&&x<h&&y>=0&&y<w&&q[x][y][num].d==-1&&maze[x][y]!=*)
 47     return true;
 48     return false;
 49 }
 50 void bfs ( int sx,int sy,int num)
 51 {
 52     q[sx][sy][num].d = 0;
 53     queue<int>x;
 54     queue<int>y;
 55     x.push(sx);
 56     y.push(sy);
 57     while (!x.empty())
 58     {
 59     int px = x.front();x.pop();
 60     int py = y.front();y.pop();
 61     //cout<<"px:"<<px<<" py:"<<py<<endl;
 62     for ( int i = 0 ; i < 4 ; i ++ )
 63     {
 64         int nx = px + dx[i];
 65         int ny = py + dy[i];
 66 //       cout<<"nx:"<<nx<<" ny:"<<ny<<endl;
 67         if (ok(nx,ny,num))
 68         {
 69     //    cout<<"why???"<<endl;
 70           if (maze[nx][ny]==#)
 71         {
 72 //              q[nx][ny][num].d = q[px][py][num].d+1;
 73             q[nx][ny][num].prex = px;
 74             q[nx][ny][num].prey = py;
 75          //   maze[nx][ny]=‘.‘;
 76         }
 77         else
 78         {
 79  //             q[nx][ny][num].d = q[px][py][num].d;
 80             q[nx][ny][num].prex = px;
 81              q[nx][ny][num].prey = py;
 82         }
 83         x.push(nx);
 84         y.push(ny);
 85         }
 86     }
 87     }
 88 }
 89 void solve ( int x,int y)
 90 {
 91 //    cout<<"x:"<<x<<" y:"<<y<<endl;
 92     if (q[x][y][0].prey!=-1&&q[x][y][0].prex!=-1)
 93     {
 94     solve(q[x][y][0].prex,q[x][y][0].prey);
 95     if (!v[x][y]&&maze[x][y]==#)
 96     {
 97         sum++;
 98 //        cout<<"sum:"<<sum<<endl;
 99         v[x][y] = true;
100     }
101     }
102 }
103 void solve2( int x,int y)
104 {
105 //      cout<<"x:"<<x<<" y:"<<y<<endl;
106     if (q[x][y][1].prex!=-1&&q[x][y][1].prey!=-1)
107     {
108     solve2(q[x][y][1].prex,q[x][y][1].prey);
109     if (!v[x][y]&&maze[x][y]==#)
110     {
111         sum++;
112         v[x][y] =true;
113     }
114     }
115 }
116 int main()
117 {
118     int T;
119     cin>>T;
120     while (T--)
121     {
122     scanf("%d %d",&h,&w);
123     int cnt  = 0;
124     for ( int i = 0 ; i < h ; i ++ )
125     {
126         cin>>maze[i];
127     }
128     for ( int i =  0 ; i < h ; i ++ )
129     {
130         for ( int j = 0 ; j  <  w ; j ++ )
131         {
132         if (maze[i][j]==$)
133         {
134              cnt++;
135              tar[cnt][0] = i;
136               tar[cnt][1] = j;
137             // cout<<"tarx:"<<i<<" tary:"<<j<<endl;
138          } 
139         }
140     }
141     cnt  = 0;
142     for ( int i = 0 ; i < h ; i ++ )
143     {
144         if (maze[i][0]==.||maze[i][0]==#)
145         {
146          cnt++;
147         beg[cnt][0]=i;
148         beg[cnt][1]=0;
149         }
150         if (maze[i][w-1]==.||maze[i][w-1]==#)
151         {
152          cnt++;
153         beg[cnt][0]=i;
154         beg[cnt][1]=w-1;
155         }
156     }
157     for ( int j = 0 ; j < w ; j ++)
158     {
159         if (maze[0][j]==.||maze[0][j]==#)
160         {
161           cnt++;
162         beg[cnt][0]=0;
163         beg[cnt][1]=j;
164         }
165         if (maze[h-1][j]==.||maze[h-1][j]==#)
166         {
167           cnt++;
168         beg[cnt][0]=h-1;
169         beg[cnt][1]=j;
170         }
171     }
172     memset(q,-1,sizeof(q));
173     bfs(tar[1][0],tar[1][1],0);
174     bfs(tar[2][0],tar[2][1],1);
175     int ans = inf;
176     for ( int i =  1; i  <= cnt ; i ++)
177     {
178         int tmpx = beg[i][0];
179         int tmpy = beg[i][1];
180      //   if (q[tmpx][tmpy][0].d==-1||q[tmpx][tmpy][1].d==-1) continue;
181         sum = 0;
182         memset(v,false,sizeof(v));
183         solve(tmpx,tmpy);    
184         solve2(tmpx,tmpy);
185 //        cout<<"sum:"<<sum<<endl;
186         ans = min (ans,sum);
187     }
188     cout<<ans<<endl;
189     }
190     return 0;
191 }
View Code

 

whust #0.2 J Jailbreak (未AC)

标签:

原文地址:http://www.cnblogs.com/111qqz/p/4716275.html

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