whust #0.2 J Jailbreak (未AC)

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: [email protected]
  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 }

时间: 2024-08-12 04:40:13

whust #0.2 J Jailbreak (未AC)的相关文章

2015年NEUACM一月月赛 J: Eliminate zero AC

问题 J: Eliminate zero AC 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Last night,Kid submitted a problem for many times but he got many WA,so he is sad.Out of sympathy, his coach gave him a very simple problem so that Kid can solve it quickly. The problem is to sel

九度OJ 1016 火星A + B 未AC版,整型存储不下

#include <iostream> #include <string.h> #include <sstream> #include <math.h> #include <vector> #include <algorithm> using namespace std; int susuTable[28]={1}; bool isLear(int num) { if(num==2) return true; else { int i

java-int类型:int默认为0导致更新操作未赋值的情况下将值更新为0

日常开发中,做更新操作的时候的处理方法为:当这个字段有值则更新,没有值就不更新,在mybatis的xml中表现为: <!-- 修改记录,只修改只不为空的字段 --> <update id="updateBySelective" parameterType="Object" > update tb_mj_user set <trim suffixOverrides="," > <if test="

whust #0.2 I Incognito

I17IncognitoSpies use attributes to disguise themselves to make sure thatthey are not recognized. For example, when putting on sun-glasses, a spy suddenly looks completely different and cannotbe recognized anymore. Every combination of attributes giv

IE10-IE11在NET4.0下出现“__doPostBack未定义”解决方案

IE10在NET4.0下出现"__doPostBack未定义"的办法 参考文章: http://blogs.msdn.com/b/scott_hanselman/archive/2011/10/28/asp-net-ie10-dopostback-javascript-ff5.aspx 方法一.浏览器设置成兼容模式. 方法二.安装服务器版的.Net40的补丁. http://pan.baidu.com/s/1hqnCQ7U 包括适用于x86和x64 方法三.点击VisualSutdio

whust #0.1 I - Laughing Out Loud

I - Laughing Out Loud Time Limit:1000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u Submit Status Practice Gym 100589I Description standard input/output Little Toojee was a happy go lucky boy. He seemed to find most, if not all, thing

(未AC)7-5 特殊队列 (30分)

普通的队列仅有 EnQueue 和 DeQueue 两种操作,分别表示在队尾增加元素和取出队首元素.现在给队列增加一种新的操作 DeleteMid,表示删除队列的中间元素.对于有 N 个元素的队列,若 N 为偶数,中间元素定义为从队首到队尾的第 N/2 个元素:若 N 为奇数,中间元素定义为第 (N+1)/2 个元素.现给出队列的一系列操作,输出相应结果. 输入格式: 第一行输入一个不超过 10?6?? 的正整数 M 和 N,分别表示指令条数和队列容量. 之后 M 行,每行给出一条指令,为下列3

POJ 2718 Smallest Difference 未AC 《挑战程序设计竞赛》

题目:POJ 2718 思路: 分为奇数和偶数两种情况进行处理,输入个数为奇数的时候,无须穷举,最小差是一定的,如0 1 2 3 4,最小差为102 - 43. 输入个数为偶数的时候,用next_permutation穷举. 没有AC-- 1 #include <iostream> 2 #include <algorithm> 3 #include <iostream> 4 #include <stdio.h> 5 #include <string.h

NOIp 2014 #2 联合权值 Label:图论 !!!未AC

题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离.对于图G 上的点对( u, v) ,若它们的距离为2 ,则它们之间会产生Wu ×Wv 的联合权值. 请问图G 上所有可产生联合权值的有序点对中,联合权值最大的是多少?所有联合权值之和是多少? 输入输出格式 输入格式: 输入文件名为link .in. 第一行包含1 个整数n . 接下来n - 1 行,