HIHOCODER#1094 : Lost in the City
2015-10-29 15:54
411 查看
时间限制: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.
样例输入
样例输出
思路:该题的难度不大,主要注意的就是题目中提到的最上一行的方向未知的问题,把这个考虑进去,程序基本就没有问题了。PS:讨论区中不少是采用矩阵旋转的方法做的,而我是采用分别四种情况直接匹配的方式。首先判断该点与模板的中间点的值是否相等。若相等,则比较周围的8个点是否也相等,从而得出结果。
PS:一开始我是用了多级if嵌套的形式,但是在hihocoder中一直出现WA,后来改为将判断条件全部写在同一个if语句后,就AC了,有点奇怪。
原来的代码:
时间限制: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
思路:该题的难度不大,主要注意的就是题目中提到的最上一行的方向未知的问题,把这个考虑进去,程序基本就没有问题了。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; }如果有知道答案的可在留言中指出,谢谢!
相关文章推荐
- Linux 虚拟串口及 Qt 串口通信实例
- 语音文法
- uploadify插件实现多个图片上传并预览
- C++ map的基本操作和使用
- ViewConfiguration 使用
- hdu3127WHUgirls【二维完全背包】
- solr5.3.1修改默认的访问字段:文件:solrconfig.xml中
- (小知识点)android 获取json语句中数组的某一个值
- 新产品孕育记:PM如何把一款产品从0带到1
- IIS7后的匿名用户IUSR与匿名用户组IIS_IUSRS
- android用户界面之ProgressBar教程实例汇总
- objc消息逆向分析
- 计算机环境要求
- linux下mysql开启远程访问权限及防火墙开放3306端口
- 对rtmp或flv协议中音频的封包的一点补充
- c# 开发CAD 只初始化窗口一次,并修改其内的数据
- vc6.0中warning C4786:identifier was truncated to '255' characters in the debug information的原因及解决办法
- oc文件操作
- sql语句中判断 case when的用法
- 理解openstack网络