hihoCoder题库1094Lost in the City

题目

时间限制: 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
我的代码(Wrong Answer)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

bool check(char a[][205],int i,int j,char b[][5]){			
	if(a[i-1][j-1]==b[0][0]){				
		if(a[i-1][j]==b[0][1]){					
			if(a[i-1][j+1]==b[0][2]){						
				if(a[i][j-1]==b[1][0]){								
					if(a[i][j+1]==b[1][2]){
						if(a[i+1][j-1]==b[2][0]){										
							if(a[i+1][j]==b[2][1]){											
								if(a[i+1][j+1]==b[2][2]){												
									return true;										
								}										
							}									
						}								
					}							
				}						
			}					
		}
	}
	return false;
}

void change(char a[][5]){           //右转90度
	int i,j;
	char b[3][3];
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			b[i][j]=a[i][j];      //复制数组
		}
	}
	for(i=0;i<3;i++){
		a[i][2]=b[0][i];
	}
	for(i=0;i<3;i++){
		a[i][1]=b[1][i];
	}
	for(i=0;i<3;i++){
		a[i][0]=b[2][i];
	}
}

int main(){
	int N,M,i,j;
	char port[5][5],port1[5][5],port2[5][5],port3[5][5];
	cin>>N>>M;
	char map[205][205];
	for(i=0;i<N;i++){
		scanf("%s", map[i]);
	}
	for(i=0;i<3;i++){
		scanf("%s", port[i]);
	}
	for(i=0;i<3;i++){                    //port向右转90度
		for(j=0;j<3;j++){
			port1[i][j]=port[i][j];
		}	
	}
	change(port1);
	for(i=0;i<3;i++){                    //port向右转180度
		for(j=0;j<3;j++){
			port2[i][j]=port1[i][j];	
		}
	}
	change(port2);
	for(i=0;i<3;i++){                    //port向右转270度
		for(j=0;j<3;j++){
			port3[i][j]=port2[i][j];	
		}
	}
	change(port3);
	for(i=0;i<N;i++){
		for(j=0;j<M;j++){
			if(map[i][j]==port[1][1]){
				if(check(map,i,j,port)||check(map,i,j,port1)||check(map,i,j,port2)||check(map,i,j,port3)){
					cout<<i+1<<" "<<j+1<<endl;
				}
			}		
		}
	}
	//system("pause");
	return 0;
}

代码一开始就是wrong answer,然后我就去查到了这位博主的代码

http://blog.csdn.net/u014355480/article/details/43459097,

然后改进了我的旋转和输入部分的代码,仔细对比之后也觉得没有差,但是他的能AC我的就是WA,搞不太懂,先放在这里。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

问题已经解决了,是我没有考虑边界

最后一部分的代码贴在下面(结果已A)

for(i=1;i<N;i++){
		for(j=1;j<M;j++){
			if(map[i][j]==port[1][1]){
				if(check(map,i,j,port)||check(map,i,j,port1)||check(map,i,j,port2)||check(map,i,j,port3)){
					cout<<i+1<<" "<<j+1<<endl;
				}
			}		
		}
	}
map[i][j]的起始搜索只能从(1,1)开始,到(N-1,M-1)处,边界没有意义。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值