#1094 : Lost in the City by C solution

 

时间限制: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

代码实现:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 int compare(char** s1, int i, int j, char surround[][3])
 5 {
 6     for (int a = i; a<3 + i; a++)
 7         for (int b = j; b<3 + j; b++)
 8         {
 9             if (s1[a][b] != surround[a - i][b - j])
10                 return 0;
11         }
12         
13     return 1;
14 }
15 int main()
16 {
17     int n = 0, m = 0;
18     scanf("%d %d", &n,&m);
19     getchar();
20     char** map = (char**)malloc(sizeof(char)*n);
21     for (int i = 0; i<n; i++)
22     {
23         map[i] = (char*)malloc(sizeof(char)*m);
24         fgets(map[i], m+1, stdin);
25         getchar();
26     }
27     char surround[3][3] = { 0 };
28     for (int i = 0; i<3; i++)
29     {
30         fgets(surround[i], 4, stdin);
31         getchar();
32     }
33     char surround1[3][3] = { 0 };
34     char surround2[3][3] = { 0 };
35     char surround3[3][3] = { 0 };
36     char position = surround[1][1];
37     for (int i = 0; i<3; i++)
38     {
39         for (int j = 0; j<3; j++)
40         {
41             surround1[2 - j][i] = surround[i][j];
42         }
43     }
44     for (int i = 0; i<3; i++)
45     {
46         for (int j = 0; j<3; j++)
47         {
48             surround2[2 - j][i] = surround1[i][j];
49         }
50     }
51     for (int i = 0; i<3; i++)
52     {
53         for (int j = 0; j<3; j++)
54         {
55             surround3[2 - j][i] = surround2[i][j];
56         }
57     }
58     for (int i = 1; i<n - 1; i++)
59     {
60         for (int j = 1; j<m - 1; j++)
61         {
62             
63             if (map[i][j] == position)
64             {
65                 if (compare(map, i - 1, j - 1, surround))
66                 {
67                     printf("%d %d\n", i + 1, j + 1);
68                     continue;
69                 }
70                 if (compare(map, i - 1, j - 1, surround1))
71                 {
72                     printf("%d %d\n", i + 1, j + 1);
73                     continue;
74                 }
75                 if (compare(map, i - 1, j - 1, surround2))
76                 {
77                     printf("%d %d\n", i + 1, j + 1);
78                     continue;
79                 }
80                 if (compare(map, i - 1, j - 1, surround3))
81                 {
82                     printf("%d %d\n", i + 1, j + 1);
83                     continue;
84                 }
85             }
86         }
87     }
88     return 0;
89 }

问题:

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

posted @ 2017-11-01 16:33  Blue_Keroro  阅读(231)  评论(0编辑  收藏  举报