# kuangbin专题一 简单搜索

A - 棋盘问题

Input

Output


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 10;
char mp[_max][_max];
bool vis[_max];
ll ans,n,k;
void dfs(int cnt,int x){
if(cnt == k){
ans++;
return ;
}
for(int i = x ; i <= n ; i++)
for(int j = 1 ; j <= n ; j++){
if(mp[i][j] == '.' || !vis[j]) continue;
vis[j] = 0;
dfs(cnt+1,i+1);
vis[j] = 1;
}
}
int main(){
while(cin>>n>>k){
if(n == -1 && k == -1) break;
memset(vis,1,sizeof(vis));
ans = 0;
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= n ; j++)
cin>>mp[i][j];
dfs(0,1);
cout<<ans<<endl;
}
return 0;
}

B - Dungeon Master
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 35;
int dx[]={0,0,0,0,1,-1};
int dy[]={0,0,1,-1,0,0};
int dz[]={1,-1,0,0,0,0};
struct node{
int x,y,z;
int step,p_step;
void init(node n1,int i){
this->x = n1.x+dx[i];
this->y = n1.y+dy[i];
this->z = n1.z+dz[i];
this->step = n1.step+1;
}
bool operator < (const node &n1)const{
return step > n1.step;
}
};
char mp[_max][_max][_max];
bool vis[_max][_max][_max];
int l,r,c;
bool check(node n1){
if(n1.x < 1 || n1.x > l) return false;
if(n1.y < 1 || n1.y > r) return false;
if(n1.z < 1 || n1.z > c) return false;
if(!vis[n1.x][n1.y][n1.z]) return false;
return true;
}
int bfs(int sx,int sy,int sz,int ex,int ey,int ez){
priority_queue<node> q;
while(!q.empty()) q.pop();
node now,next;
now.x = sx,now.y = sy,now.z = sz;
now.step = 0;
vis[now.x][now.y][now.z] = 0;
q.push(now);
while(!q.empty()){
now = q.top();
q.pop();
for(int i = 0 ; i < 6 ; i++){
next.init(now,i);
if(!check(next)) continue;
vis[next.x][next.y][next.z] = 0;
if(next.x == ex && next.y == ey && next.z == ez){
while(!q.empty()) q.pop();
return next.step;
}
q.push(next);
}
}
return -1;
}
int main(){
IOS;
int sx,sy,sz,ex,ey,ez;
while(cin>>l>>r>>c){
if(!l && !r && !c) break;
memset(vis,0,sizeof(vis));
for(int t = 1 ; t <= l ; t++){
for(int i = 1 ; i <= r ; i++)
for(int j = 1 ; j <= c ; j++){
cin>>mp[t][i][j];
if(mp[t][i][j] == 'S'){
sx = t;
sy = i;
sz = j;
}
if(mp[t][i][j] == 'E'){
ex = t;
ey = i;
ez = j;
}
if(mp[t][i][j] != '#')
vis[t][i][j] = 1;
}
}
int cnt = bfs(sx,sy,sz,ex,ey,ez);
if(cnt < 0) cout<<"Trapped!"<<endl;
else cout<<"Escaped in "<<cnt<<" minute(s)."<<endl;
}
return 0;
} 

C - Catch That Cow

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

• Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
• Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 100005;
bool vis[_max];
struct node{
int x,t;
};
int bfs(int n,int k){
vis[n] = 0;
queue<node> q;
while(!q.empty()) q.pop();
node now,next;
now.x = n,now.t = 0;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 1 ; i <= 3 ; i++){
next.x = -1;
if(i == 1 && now.x+1 < _max && vis[now.x+1])
next.x = now.x+1,vis[next.x] = 0;
else if(i == 2 && now.x-1 >=0 && vis[now.x-1])
next.x = now.x-1,vis[next.x] = 0;
else if(i == 3 && now.x*2 < _max && vis[now.x*2])
next.x = now.x*2,vis[next.x] = 0;
if(next.x == -1) continue;
next.t = now.t+1;
if(next.x == k)
return next.t;
q.push(next);
}
}
}
int main(){
int n,k;
while(cin>>n>>k){
memset(vis,1,sizeof(vis));
if(n < k)
cout<<bfs(n,k)<<endl;
else
cout<<abs(k-n)<<endl;
}
return 0;
}

D - Fliptile
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.

As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.

Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word “IMPOSSIBLE”.

Input
Line 1: Two space-separated integers: M and N
Lines 2.. M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white
Output
Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 20;
bool mp[_max][_max];
bool ans[_max][_max];
bool rec[_max][_max];
int n,m;
int dx[]={0,0,0,1,-1};
int dy[]={1,-1,0,0,0};
bool check(int x,int y){
int cnt = mp[x][y];
for(int i = 0 ; i < 5 ; i++){
if(x+dx[i] < 1 || x+dx[i] >n) continue;
if(y+dy[i] < 1 || y+dy[i] >m) continue;
cnt+=rec[x+dx[i]][y+dy[i]];
}
return cnt%2;
}
int dfs(){
int cnt = 0;
for(int i = 2 ; i <= n ; i++)
for(int j = 1 ; j <= m ; j++)
if(check(i-1,j))
rec[i][j] = 1;
for(int i = 1 ; i <= m ; i++)
if(check(n,i)) return -1;
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= m ; j++)
cnt+=rec[i][j];
return cnt;
}
int main(){
while(cin>>n>>m){
int maxn = inf;
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= m ; j++)
cin>>mp[i][j];
for(int sta = 0 ; sta < (1<<m) ; sta++){
memset(rec,0,sizeof(rec));
for(int i = 0 ; i < m ; i++)
rec[1][i+1] = (sta>>i)&1;
int cnt = dfs();
if(cnt >= 0 && cnt < maxn){
maxn = cnt;
memcpy(ans,rec,sizeof(rec));
}
}
if(maxn == inf){
cout<<"IMPOSSIBLE"<<endl;
continue;
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++)
cout<<ans[i][j]<<' ';
cout<<endl;
}
}
return 0;
}

E - Find The Multiple

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.
Input
The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.
Output
For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 105;
bool vis[_max*2];
struct node{
string s;
ll m;
};
int n;
queue<node> q;
node bfs(){
while(!q.empty()) q.pop();
node now,next;
now.s = "1";
now.m = 1%n;
if(!now.m) return now;
vis[now.m] = 0;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 0 ; i <= 1; i++){
next.s = now.s + char('0'+i);
next.m = (now.m*10+i)%n;
if(!next.m) return next;
if(!vis[next.m]) continue;
vis[next.m] = 0;
q.push(next);
}
}
}
int main(){
while(cin>>n && n){
memset(vis,1,sizeof(vis));
cout<<bfs().s<<endl;
}
return 0;
}

F - Prime Path
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.

Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
Input
One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).
Output
One line for each case, either with a number stating the minimal cost or containing the word Impossible.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 10000;
struct node{
int x,t;
};
bool vis[_max];
bool vis1[_max];
int t,a,b;
void init(){
memset(vis1,1,sizeof(vis));
vis1[1] = 0;
for(int i = 2 ; i*i < _max ; i++){
if(!vis1[i]) continue;
for(int k = 2 ; k*i < _max ; k++){
vis1[i*k] = 0;
}
}
return ;
}
int bfs(int a,int b){
queue<node> q;
while(!q.empty()) q.pop();
node now,next;
now.x = a,now.t = 0;
if(now.x == b) return now.t;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 1 ; i <= 1000 ; i*=10){
int s = (now.x/i)%10;
for(int j = 0 ; j <= 9 ; j++){
next.x = now.x-s*i+j*i;
if(next.x > 9999 || next.x < 1000) continue;
next.t = now.t+1;
if(!vis1[next.x]) continue;
if(!vis[next.x]) continue;
vis[next.x] = 0;
if(next.x == b) return next.t;
q.push(next);
}
}
}
return -1;
}
int main(){
cin>>t;
while(t--){
init();
memset(vis,1,sizeof(vis));
cin>>a>>b;
cout<<bfs(a,b)<<endl;
}
return 0;
}

G - Shuffle’m Up
A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several different colors.

The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:

The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.

After the shuffle operation, S12 is split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new S2. The shuffle operation may then be repeated to form a new S12.

For this problem, you will write a program to determine if a particular resultant stack S12 can be formed by shuffling two stacks some number of times.

Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.

Each dataset consists of four lines of input. The first line of a dataset specifies an integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S1 and S2 zero or more times. The bottommost chip’s color is specified first.

Output
Output for each dataset consists of a single line that displays the dataset number (1 though N), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (−1) for the number of shuffle operations.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;

map<string,bool> vis;
string s1,s2,s12;
int n;

int main(){
IOS;
int t,Cas=1;
cin>>t;
while(t--){
cin>>n>>s1>>s2>>s12;
vis.clear();
int t = 0;
while(1){
string cnt = "";
for(int i = 0 ; i < n ; i++){
cnt+=s2[i];
cnt+=s1[i];
}
t++;
if(cnt == s12){
cout<<Cas++<<' '<<t<<endl;
break;
}
if(vis[cnt] == 1){
cout<<Cas++<<' '<<-1<<endl;
break;
}
vis[cnt] = 1;
s1 = cnt.substr(0,n);
s2 = cnt.substr(n);
}
}
return 0;
}

H - Pots
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

1.装满1号杯子
2.清空1号杯子
3.装满2号杯子
4.清空2号杯子
5.1号给2号杯子倒水（自己倒完或者2号杯子满了）
6.2号给1号杯子倒水（自己倒完或者1号杯子满了）

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 105;

bool vis[_max][_max];
struct node{
int a,b;
int step;
int t;
};
int da[]={1,2,0,0,1,-1};
int db[]={0,0,1,2,-1,1};
node mp[_max][_max];
int a,b,c;
node solve(node n1,int i){
n1.t++;
n1.step = i;
if(da[i] == 0){
if(db[i] == 1){
n1.b = b;
return n1;
}
else{
n1.b = 0;
n1.step = i;
return n1;
}
}
if(db[i] == 0){
if(da[i] == 1){
n1.a = a;
return n1;
}
else{
n1.a = 0;
n1.step = i;
return n1;
}
}
if(da[i] == -1){
if(n1.a > b-n1.b){
n1.a -= (b-n1.b);
n1.b = b;
}
else{
n1.b += n1.a;
n1.a = 0;
}
return n1;
}
if(db[i] == -1){
if(n1.b > a-n1.a){
n1.b -= (a-n1.a);
n1.a = a;
}
else{
n1.a += n1.b;
n1.b = 0;
}
return n1;
}
}
node bfs(){
queue<node> q;
while(!q.empty()) q.pop();
node now,next;
now.a = 0, now.b = 0, now.step = -2;
now.t = 0;
vis[now.a][now.b] = 0;
mp[now.a][now.b] = now;
if(now.a == c || now.b == c) return now;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 0 ; i < 6 ; i++){
next = solve(now,i);
if(!vis[next.a][next.b]) continue;
vis[next.a][next.b] = 0;
mp[next.a][next.b] = now;
mp[next.a][next.b].step = next.step;
if(next.a == c || next.b == c) return next;
q.push(next);
}
}
now.step = -3;
return now;
}
void printf_ans(node n1){
if(n1.a != 0 || n1.b != 0){
printf_ans(mp[n1.a][n1.b]);
}
//  cout<<n1.a<<' '<<n1.b<<' '<<n1.step<<endl;
if(n1.step == 0) cout<<"FILL(1)"<<endl;
else if(n1.step == 1) cout<<"DROP(1)"<<endl;
else if(n1.step == 2) cout<<"FILL(2)"<<endl;
else if(n1.step == 3) cout<<"DROP(2)"<<endl;
else if(n1.step == 4) cout<<"POUR(2,1)"<<endl;
else if(n1.step == 5) cout<<"POUR(1,2)"<<endl;
}
int main(){
IOS;
while(cin>>a>>b>>c){
memset(vis,1,sizeof(vis));
node cnt = bfs();
if(cnt.step == -3){
cout<<"impossible"<<endl;
continue;
}
cout<<cnt.t<<endl;
printf_ans(mp[cnt.a][cnt.b]);
}
return 0;
}

I - Fire Game
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

Input
The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

Output
For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 15;
char mp[_max][_max];
bool vis[_max][_max];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node{
int x,y;
int t;
};
int n,m,len;
queue<node> q;
node num[_max*_max];
bool check(node n1){
if(n1.x < 1 || n1.x > n) return false;
if(n1.y < 1 || n1.y > m) return false;
if(!vis[n1.x][n1.y]) return false;
if(mp[n1.x][n1.y] == '.') return false;
return true;
}
int bfs(int cnt){
int t = 0;
int c = 2;
if(c == len) return t;
node now,next;
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 0 ; i < 4 ; i++){
next.x = now.x+dx[i];
next.y = now.y+dy[i];
next.t = now.t+1;
if(!check(next)) continue;
if(next.t >= cnt) return cnt;
t = max(t,next.t);
c++;
vis[next.x][next.y] = 0;
if(c == len) return t;
q.push(next);
}
}
return inf;
}
int main(){
IOS;
int T,Cas=0;
cin>>T;
while(T--){
cin>>n>>m;
len = 0;
memset(vis,1,sizeof(vis));
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= m ; j++){
cin>>mp[i][j];
if(mp[i][j] == '#'){
num[++len].x = i;
num[len].y = j;
num[len].t = 0;
}
}
int cnt = inf;
if(len <= 2) cnt = 0;
for(int i = 1 ; i <= len ; i++)
for(int j = i+1 ; j <= len ; j++){
memset(vis,1,sizeof(vis));
while(!q.empty()) q.pop();
vis[num[i].x][num[i].y] = false;
vis[num[j].x][num[j].y] = false;
q.push(num[i]);
q.push(num[j]);
cnt = min(cnt,bfs(cnt));
}
cout<<"Case "<<++Cas<<": ";
if(cnt == inf) cout<<-1<<endl;
else cout<<cnt<<endl;

}
return 0;
} 

J - Fire!
square that borders the edge of the maze. Neither Joe nor the fire
may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test
cases to follow. The first line of each test case contains the two
integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The
following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
C characters, and each of these characters is one of:
• #, a wall
• ., a passable square
• J, Joe’s initial position in the maze, which is a passable square
• F, a square that is on fire
There will be exactly one J in each test case.
Output
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 1050;
char mp[_max][_max];
bool vis[_max][_max];
int f[_max][_max];
int n,m;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node{
int x,y;
int t;
};
queue<node> q;
void bfs_fire(){
node now,next;
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 0 ; i < 4 ; i++){
next.x = now.x+dx[i];
next.y = now.y+dy[i];
next.t = now.t+1;
if(mp[next.x][next.y] == '#') continue;
if(!vis[next.x][next.y]) continue;
if(next.x < 1 || next.x > n) continue;
if(next.y < 1 || next.y > m) continue;
vis[next.x][next.y] = 0;
f[next.x][next.y] = next.t;
q.push(next);
}
}
}
int bfs(int sx,int sy){
while(!q.empty()) q.pop();
node now,next;
now.x = sx, now.y = sy;
now.t = 0;
vis[now.x][now.y] = 0;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 0 ; i < 4 ; i++){
next.x = now.x+dx[i];
next.y = now.y+dy[i];
next.t = now.t+1;
if(next.x < 1 || next.x > n) return next.t;
if(next.y < 1 || next.y > m) return next.t;
if(mp[next.x][next.y] == '#') continue;
if(!vis[next.x][next.y]) continue;
if(f[next.x][next.y] <= next.t) continue;
vis[next.x][next.y] = 0;
q.push(next);
}
}
return -1;
}
int main(){
IOS;
int t,sx,sy;
cin>>t;
while(t--){
cin>>n>>m;
memset(f,1,sizeof(f));
while(!q.empty()) q.pop();
memset(vis,1,sizeof(vis));
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= m ; j++){
cin>>mp[i][j];
if(mp[i][j] == 'J'){
sx = i;
sy = j;
}
if(mp[i][j] == 'F'){
node n1;
n1.x = i; n1.y = j;
n1.t = 0;
vis[n1.x][n1.y] = 0;
f[n1.x][n1.y] = 0;
q.push(n1);
}
}

bfs_fire();
/*  for(int i = 1 ; i <= n ; i++){
for(int j = 1 ;j  <= m ; j++)
cout<<f[i][j]<<' ';
cout<<endl;
}
*/
memset(vis,1,sizeof(vis));
int cnt = bfs(sx,sy);
if(cnt == -1) cout<<"IMPOSSIBLE"<<endl;
else cout<<cnt<<endl;
}
return 0;
}

K - 迷宫问题

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,


};

Input

Output

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 10;
char mp[_max][_max];
int n = 5;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
bool vis[_max][_max];
struct node{
int x,y;
int px,py;
};
node tra[_max][_max];
bool check(node n1){
if(mp[n1.x][n1.y] == '1') return false;
if(n1.x < 1 || n1.x > n) return false;
if(n1.y < 1 || n1.y > n) return false;
if(!vis[n1.x][n1.y]) return false;
return true;
}
void bfs(int sx,int sy,int ex,int ey){
queue<node> q;
while(!q.empty()) q.pop();
node now,next;
now.x=sx,now.y=sy;
now.px=sx,now.py=sy;
tra[1][1] = now;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 0 ; i < 4 ; i++){
next.x = now.x+dx[i];
next.y = now.y+dy[i];
next.px = now.x;
next.py = now.y;
if(!check(next)) continue;
vis[next.x][next.y] = 0;
tra[next.x][next.y] = next;
if(next.x == n && next.y == n) return ;
q.push(next);
}
}
}
void printf_ans(int x,int y){
if(tra[x][y].x != tra[x][y].px || tra[x][y].y != tra[x][y].py)
printf_ans(tra[x][y].px,tra[x][y].py);
cout<<"("<<x-1<<", "<<y-1<<")"<<endl;
}
int main(){
memset(vis,1,sizeof(vis));
vis[1][1] = 0;
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= n ; j++)
cin>>mp[i][j];
bfs(1,1,n,n);
printf_ans(n,n);
return 0;
}

L - Oil Deposits
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either *', representing the absence of oil, or@’, representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 105;
struct node{
int x,y;
};
char mp[_max][_max];
bool vis[_max][_max];
int dx[] = {0,0,1,-1,1,-1,1,-1};
int dy[] = {1,-1,0,0,-1,1,1,-1};
int n,m;
bool check(node n1){
if(!vis[n1.x][n1.y]) return false;
if(n1.x < 1 || n1.x > n) return false;
if(n1.y < 1 || n1.y > m) return false;
return true;
}
void bfs(int x,int y){
queue<node> q;
while(!q.empty()) q.pop();
node now,next;
now.x = x,now.y = y;
q.push(now);
vis[x][y] = 0;
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 0 ; i < 8 ; i++){
next.x = now.x+dx[i];
next.y = now.y+dy[i];
if(!check(next)) continue;
vis[next.x][next.y] = 0;
q.push(next);
}
}

}
int main(){
while(cin>>n>>m && n&&m){
int cnt = 0;
memset(vis,1,sizeof(vis));
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= m ; j++){
cin>>mp[i][j];
if(mp[i][j] == '*')
vis[i][j] = 0;
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
if(!vis[i][j]) continue;
bfs(i,j);
cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}

M - 非常可乐

Input

Output

Sample Input
7 4 3
4 1 3
0 0 0
Sample Output
NO
3

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 105;
struct node{
int val[3];
int t;
};
int vis[_max][_max][_max];
int v[3];
int da[]={0,0,1,1,2,2};
int db[]={1,2,0,2,0,1};
node push(node n1,int a,int b){
int va = n1.val[a];
int vb = n1.val[b];
if(va > v[b]-vb){
n1.val[a] -= (v[b]-vb);
n1.val[b] = v[b];
}
else{
n1.val[b] += n1.val[a];
n1.val[a] = 0;
}
n1.t++;
return n1;
}
bool check(node n1){
if(n1.val[0]&&n1.val[1]&&n1.val[2]) return true;
for(int i = 0 ; i < 3 ; i++){
for(int j = i+1 ; j < 3 ; j++){
if(i == j) continue;
if(n1.val[i] == n1.val[j])
return false;
}
}
return true;
}
int bfs(int a,int b,int c){
queue<node> q;
while(!q.empty()) q.pop();
node now,next;
now.val[0] = a,now.val[1] = 0,now.val[2] = 0;
now.t = 0;
vis[now.val[0]][now.val[1]][now.val[2]] = 0;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
for(int i = 0 ; i < 6 ; i++){
next = push(now,da[i],db[i]);
if(!vis[next.val[0]][next.val[1]][next.val[2]]) continue;
if(!check(next)) return next.t;
vis[next.val[0]][next.val[1]][next.val[2]] = 0;
q.push(next);
}
}
return -1;
}
int main(){
while(cin>>v[0]>>v[1]>>v[2]){
if(!v[0]&&!v[1]&&!v[2]) break;
memset(vis,1,sizeof(vis));
int cnt = bfs(v[0],0,0);
if(cnt == -1) cout<<"NO"<<endl;
else cout<<cnt<<endl;
}
return 0;
}

N - Find a way
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘@’ KCF
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 205;
struct node{
int x,y;
int t;
};
char mp[_max][_max];
bool vis[_max][_max];
int c[_max][_max];
int c1[_max][_max];
int n,m;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
bool check(node n1){
if(n1.x < 1 || n1.x > n) return false;
if(n1.y < 1 || n1.y > m) return false;
if(!vis[n1.x][n1.y]) return false;
if(mp[n1.x][n1.y] == '#') return false;
if(mp[n1.x][n1.y] == 'Y') return false;
if(mp[n1.x][n1.y] == 'M') return false;
return true;
}
void bfs(int x,int y){
memset(vis,1,sizeof(vis));
vis[x][y] = 0;
queue<node> q;
while(!q.empty()) q.pop();
node now,next;
now.x = x,now.y = y;
now.t = 0;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
//  cout<<now.x<<' '<<now.y<<' '<<now.t<<endl;
for(int i = 0 ; i < 4 ; i++){
next.x = now.x+dx[i];
next.y = now.y+dy[i];
next.t = now.t+1;
if(!check(next)) continue;
vis[next.x][next.y] = 0;
if(mp[next.x][next.y] == '@'){
//  c[next.x][next.y] = max(c[next.x][next.y],next.t);
c[next.x][next.y] += next.t;
c1[next.x][next.y]++;
}
q.push(next);
}
}
}
int main(){
IOS;
int x1,y1,x2,y2;
while(cin>>n>>m){
memset(c,0,sizeof(c));
memset(c1,0,sizeof(c1));
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= m ; j++){
cin>>mp[i][j];
if(mp[i][j] == 'Y'){
x1 = i;
y1 = j;
}
if(mp[i][j] == 'M'){
x2 = i;
y2 = j;
}
}
bfs(x1,y1);
bfs(x2,y2);
int cnt = inf;
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= m ; j++)
if(mp[i][j] == '@' && c1[i][j] == 2)
cnt = min(cnt,c[i][j]);
cout<<cnt*11<<endl;
}
return 0;
}