-
8 8 ...HSH.. ...HSM.. ...HST.. ...HSPP. PPGHSPPT PPSSSSSS ..MMSHHH ..MMSH.. SSS SHG SH.
样例输出
描述
Little Hi gets lost in the city. He does not know where he is. He does not know which direction is north.
Fortunately, Little Hi has a map of the city. The map can be considered as a grid of N*M blocks. Each block is numbered by a pair of integers. The block at the north-west corner is (1, 1) and the one at the south-east corner is (N, M). Each block is represented by a character, describing the construction on that block: '.' for empty area, 'P' for parks, 'H' for houses, 'S' for streets, 'M' for malls, 'G' for government buildings, 'T' for trees and etc.
Given the blocks of 3*3 area that surrounding Little Hi(Little Hi is at the middle block of the 3*3 area), please find out the position of him. Note that Little Hi is disoriented, the upper side of the surrounding area may be actually north side, south side, east side or west side.
输入
Line 1: two integers, N and M(3 <= N, M <= 200).
Line 2~N+1: each line contains M characters, describing the city's map. The characters can only be 'A'-'Z' or '.'.
Line N+2~N+4: each line 3 characters, describing the area surrounding Little Hi.
输出
Line 1~K: each line contains 2 integers X and Y, indicating that block (X, Y) may be Little Hi's position. If there are multiple possible blocks, output them from north to south, west to east.
5 4思路: 该题的难度不大,主要注意的就是题目中提到的最上一行的方向未知的问题,把这个考虑进去,程序基本就没有问题了。 PS:讨论区中不少是采用矩阵旋转的方法做的,而我是采用分别四种情况直接匹配的方式。首先判断该点与模板的中间点的值是否相等。若相等,则比较周围的8个点是否也相等,从而得出结果。
#include<iostream>
using namespace std;
char data[210][210];
char flag[4][4];
int main(){
int N,M;
cin>>N>>M;
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++)
cin>>data[i][j];
}
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++)
cin>>flag[i][j];
}
for(int i=2;i<=N-1;i++){
for(int j=2;j<=M-1;j++){
if(data[i][j]==flag[2][2]){
if((flag[1][1]==data[i-1][j-1])&&(flag[2][1]==data[i][j-1])&&(flag[3][1]==data[i+1][j-1])&&(flag[1][3]==data[i-1][j+1])&&(flag[2][3]==data[i][j+1])&&(flag[3][3]==data[i+1][j+1])&&(flag[1][2]==data[i-1][j])&&(flag[3][2]==data[i+1][j])){//north side
cout<<i<<" "<<j<<endl;
}
else if((flag[1][1]==data[i+1][j+1])&&(flag[2][1]==data[i][j+1])&&(flag[3][1]==data[i-1][j+1])&&(flag[1][3]==data[i+1][j-1])&&(flag[2][3]==data[i][j-1])&&(flag[3][3]==data[i-1][j-1])&&(flag[1][2]==data[i+1][j])&&(flag[3][2]==data[i-1][j])){//south side
cout<<i<<" "<<j<<endl;
}
else if((flag[1][1]==data[i-1][j+1])&&(flag[2][1]==data[i-1][j])&&(flag[3][1]==data[i-1][j-1])&&(flag[1][3]==data[i+1][j+1])&&(flag[2][3]==data[i+1][j])&&(flag[3][3]==data[i+1][j-1])&&(flag[1][2]==data[i][j+1])&&(flag[3][2]==data[i][j-1])){//west side
cout<<i<<" "<<j<<endl;
}
else if((flag[1][1]==data[i+1][j-1])&&(flag[2][1]==data[i+1][j])&&(flag[3][1]==data[i+1][j+1])&&(flag[1][3]==data[i-1][j-1])&&(flag[2][3]==data[i-1][j])&&(flag[3][3]==data[i-1][j+1])&&(flag[1][2]==data[i][j-1])&&(flag[3][2]==data[i][j+1])){//east side
cout<<i<<" "<<j<<endl;
}
}
}
}
return 0;
}
PS:一开始我是用了多级if嵌套的形式,但是在hihocoder中一直出现WA,后来改为将判断条件全部写在同一个if语句后,就AC了,有点奇怪。
原来的代码:
#include<iostream>
using namespace std;
char data[210][210];
char flag[4][4];
int main(){
int N,M;
cin>>N>>M;
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++)
cin>>data[i][j];
}
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++)
cin>>flag[i][j];
}
for(int i=2;i<=N-1;i++){
for(int j=2;j<=M-1;j++){
if(data[i][j]==flag[2][2]){
if((flag[1][1]==data[i-1][j-1])&&(flag[2][1]==data[i][j-1])&&(flag[3][1]==data[i+1][j-1])){//north side
if((flag[1][3]==data[i-1][j+1])&&(flag[2][3]==data[i][j+1])&&(flag[3][3]==data[i+1][j+1])){
if((flag[1][2]==data[i-1][j])&&(flag[3][2]==data[i+1][j])){
cout<<i<<" "<<j<<endl;
}
}
}
else if((flag[1][1]==data[i+1][j+1])&&(flag[2][1]==data[i][j+1])&&(flag[3][1]==data[i-1][j+1])){//south side
if((flag[1][3]==data[i+1][j-1])&&(flag[2][3]==data[i][j-1])&&(flag[3][3]==data[i-1][j-1])){
if((flag[1][2]==data[i+1][j])&&(flag[3][2]==data[i-1][j])){
cout<<i<<" "<<j<<endl;
}
}
}
else if((flag[1][1]==data[i-1][j+1])&&(flag[2][1]==data[i-1][j])&&(flag[3][1]==data[i-1][j-1])){//west side
if((flag[1][3]==data[i+1][j+1])&&(flag[2][3]==data[i+1][j])&&(flag[3][3]==data[i+1][j-1])){
if((flag[1][2]==data[i][j+1])&&(flag[3][2]==data[i][j-1])){
cout<<i<<" "<<j<<endl;
}
}
}
else if((flag[1][1]==data[i+1][j-1])&&(flag[2][1]==data[i+1][j])&&(flag[3][1]==data[i+1][j+1])){//east side
if((flag[1][3]==data[i-1][j-1])&&(flag[2][3]==data[i-1][j])&&(flag[3][3]==data[i-1][j+1])){
if((flag[1][2]==data[i][j-1])&&(flag[3][2]==data[i][j+1])){
cout<<i<<" "<<j<<endl;
}
}
}
}
}
}
return 0;
}
如果有知道答案的可在留言中指出,谢谢!