The Proper Key
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 164 Accepted Submission(s): 41
Problem Description
Many people think that Tetris was invented by two Russian programmers. But that is not the whole truth. The idea of the game is very old -- even the Egyptians had something similar. But they did not use it as a game. Instead, it was used as a very complicated lock. The lock was made of wood and consisted of a large number of square fields, laid out in regular rows and columns. Each field was either completely filled with wood, or empty. The key for this lock was two-dimensional and it was made by joining square parts of the same size as the fields of the lock. So they had a 2D lock and 2D key that could be inserted into the lock from the top. The key was designed so that it was not possible to move it upwards. It could only fall down and it could slide sideways -- exactly like in a Tetris game. The only difference is that the key could not be rotated. Rotation in Tetris is really a Russian invention.
The entry gate into the Pyramid has such a lock. The ACM archaeologists have found several keys and one of them belongs to the lock with a very high probability. Now they need to try them out and find which one to use. Because it is too time-consuming to try all of them, it is better to begin with those keys that may be inserted deeper into the lock. Your program should be able to determine how deep a given key can be inserted into a given lock.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers R and C (1 <= R,C <= 100) indicating the key size. Then exactly R rows follow, each containing C characters. Each character is either a hash mark (#) or a period (.). A hash mark represents one square field made of wood; a period is an empty field. The wooden fields are always connected, i.e. the whole key is made of one piece. Moreover, the key remains connected even if we cut off arbitrary number of rows from its top. There is always at least one non-empty field in the top-most and bottom-most rows and the left-most and right-most columns.
After the key description, there is a line containing two integers D and W (1 <= D <= 10000, 1 <= W <= 1000). The number W is the lock width, and D is its depth. The next D lines contain W characters each. The character may be either a hash mark (representing the wood) or a period (the free space).
Output
Your program should print one line of output for each test case. The line should contain the statement "The key falls to depth X.". Replace X with the maximum depth to which the key can be inserted by moving it down and sliding it to the left or right only. The depth is measured as the distance between the bottom side of the key and the top side of the lock. If it is possible to move the key through the whole lock and take it away at the bottom side, output the sentence "The key can fall through.".
Sample Input
4 2 4 #.## ###. 3 6 #....# #....# #..### 2 3 ##. .## 2 7 #.#.#.# .#.#.#. 1 1 # 1 10 ###....### 3 2 ## .# .# 1 5 #.#.#
Sample Output
The key falls to depth 2. The key falls to depth 0. The key can fall through. The key falls to depth 2.
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
bool key[109][109],s[11009][1009],visit[11009][1009];
int n,m;
int nm,mm;
bool in(int y)
{
if(y>=0&&y<mm)
return true;
return false;
}
bool door(int x,int y)
{
int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
if(key[i][j]&&s[i+x][j+y])
return false;
}
return true;
}
int main()
{
int cas,i,j,flag;
char str[1009];
scanf("%d",&cas);
while(cas--)
{
memset(key,false,sizeof(key));
memset(s,false,sizeof(s));
memset(visit,false,sizeof(visit));
scanf("%d%d",&n,&m);
for(i=0; i<n; i++)
{
scanf("%s",str);
for(j=0; j<m; j++)
{
if(str[j]=='#')
key[i][j]=true;
}
}
scanf("%d%d",&nm,&mm);
nm+=n;
for(i=n; i<nm; i++)
{
scanf("%s",str);
for(j=0; j<mm; j++)
{
if(str[j]=='#')
s[i][j]=true;
}
}
for(i=0; i<mm; i++)
visit[0][i]=true;
for(i=1; i<nm; i++)
{
flag=false;
for(j=0; j<=mm-m; j++)
{
if(!visit[i][j]&&(visit[i-1][j]||(in(j-1)&&visit[i][j-1])||(in(j+1)&&visit[i][j+1])))
{
if(door(i,j))
{
flag=true;
visit[i][j]=true;
if(j>0&&!visit[i][j-1])
j-=2;
}
}
}
if(!flag)
break;
}
if(i>=nm)
printf("The key can fall through.\n");
else
printf("The key falls to depth %d.\n",i-1);
}
return 0;
}