#1094 : Lost in the City by C solution

时间:2021-10-09 11:00:41
时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

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.

样例输入
8 8
...HSH..
...HSM..
...HST..
...HSPP.
PPGHSPPT
PPSSSSSS
..MMSHHH
..MMSH..
SSS
SHG
SH.
样例输出
5 4

代码实现:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare(char** s1, int i, int j, char surround[][])
{
for (int a = i; a< + i; a++)
for (int b = j; b< + j; b++)
{
if (s1[a][b] != surround[a - i][b - j])
return ;
} return ;
}
int main()
{
int n = , m = ;
scanf("%d %d", &n,&m);
getchar();
char** map = (char**)malloc(sizeof(char)*n);
for (int i = ; i<n; i++)
{
map[i] = (char*)malloc(sizeof(char)*m);
fgets(map[i], m+, stdin);
getchar();
}
char surround[][] = { };
for (int i = ; i<; i++)
{
fgets(surround[i], , stdin);
getchar();
}
char surround1[][] = { };
char surround2[][] = { };
char surround3[][] = { };
char position = surround[][];
for (int i = ; i<; i++)
{
for (int j = ; j<; j++)
{
surround1[ - j][i] = surround[i][j];
}
}
for (int i = ; i<; i++)
{
for (int j = ; j<; j++)
{
surround2[ - j][i] = surround1[i][j];
}
}
for (int i = ; i<; i++)
{
for (int j = ; j<; j++)
{
surround3[ - j][i] = surround2[i][j];
}
}
for (int i = ; i<n - ; i++)
{
for (int j = ; j<m - ; j++)
{ if (map[i][j] == position)
{
if (compare(map, i - , j - , surround))
{
printf("%d %d\n", i + , j + );
continue;
}
if (compare(map, i - , j - , surround1))
{
printf("%d %d\n", i + , j + );
continue;
}
if (compare(map, i - , j - , surround2))
{
printf("%d %d\n", i + , j + );
continue;
}
if (compare(map, i - , j - , surround3))
{
printf("%d %d\n", i + , j + );
continue;
}
}
}
}
return ;
}

问题:

使用Visual Studio编译运行样例结果正确,但是提交代码通过G++运行会出现Wrong Answer,若知道原因的话还望请留言告知!