模拟题目HDU2599
2013-12-06 23:58
120 查看
2599 - The Dreadful Seven
时间限制: | Java: 2000 ms / Others: 1000 ms |
内存限制: | Java: 32768 KB / Others: 32768 KB |
问题描述
Super Mario is studying how to use a mysterious Power Square. The Power Square is n×n with integer values between 0 and n2-1.A number y is a neighbor of another number x in the Power Square if y is directly above or below x, or directly to the left or rightof x. Rosalina asks Super Mario to find all the numbers in the Power Square that are neighbors of the number 7, since she can tell that those numbers are quite nervous.”Why are the numbers scared of seven?” Mario asks Rosalina.”Because seven ate nine!” Rosalina
exclaims.
输入说明
Input is a description of of the Power Square, followed by a number of commands. The first line is the size of the Power Square n. You may assume n<=100. The second line contains the n2 values in the Power Square, separated by spaces. Values start from thetop left corner and move from left to right, moving down one row to the leftmost position when a row is filled.Following the Power Square description are a number of commands, with each command on a separate line. Each command begins with the name of the command,
followed by any additional command parameters.There will no more than 100 commands.
输出说明
The command ”SHOW” causes the current state of the Power Square to be displayed in n × n form (each row of n values on a single line, separated by spaces), followed by a blank line.The command ”NEIGHBORS” is followed by a value x in the Power Square. The valuesneighboring x are output and displayed on a single line (in the order: above, left, right, and below x), separated by spaces. You may assume that x is always in the Power Square.
输入样例
3 8 7 6 5 4 3 2 1 0 SHOW NEIGHBORS 7 NEIGHBORS 1 NEIGHBORS 4 4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 SHOW NEIGHBORS 7 NEIGHBORS 1 NEIGHBORS 8 NEIGHBORS 14
输出样例
8 7 6 5 4 3 2 1 0 8 6 4 4 2 0 7 5 3 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 3 6 11 0 2 5 4 9 12 10 13 15
杭电OJ平台(代理)
思路:看懂了题意就比较容易想到思路,题目大致意思是当输入show时,就输出矩阵,当输入NEIGHBORS k时就输出k的上下左右数字。
关键是中间的细节处理,总而言之,又是一道看重细节的题目,中间WA了三次,真是过于粗心了。debug点都已经标志出来了。
#include <iostream>
#include <string.h>
using namespace std;
char str[200];
int arr[103][103];
int dir[4][2]={-1,0,0,-1,0,1,1,0};
int n;
void show()
{
int i,j;
for (i=0; i<n; ++i)
{
printf("%d",arr[i][0]); //debug point:first time forget the ' '
for (j=1; j<n; ++j)
{
printf(" %d",arr[i][j]);
}
printf("\n");
}
printf("\n");
}
void neighbor(int m)
{
int i = 0,j = 0,k;
bool isFound = false;
for (i=0; !isFound && i<n; ++i)
{
for (j=0; !isFound && j<n; ++j)
{
if (arr[i][j] == m)
isFound = true;
}
}
--i,--j;
bool isFirstTime = true;
for (k=0; k<4; ++k)
{
if (0 <= i+dir[k][0] && i+dir[k][0] <n && 0 <= j+dir[k][1] && j+dir[k][1] <n)
{
if (!isFirstTime)
printf(" %d",arr[i+dir[k][0]][j+dir[k][1]]); // debug point
else
{
printf("%d",arr[i+dir[k][0]][j+dir[k][1]]);
isFirstTime = false;
}
}
}
puts("");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("2.txt","r",stdin);
#endif
int i,j,m;
while (scanf("%s",str)!=EOF)
{
if (strcmp(str,"SHOW") && strcmp(str,"NEIGHBORS"))
{
n = atoi(str);
for (i=0; i<n; ++i)
for (j=0; j<n; ++j)
scanf("%d",&arr[i][j]);
getchar();
}
else
{
if (strcmp(str,"SHOW")==0)
show();
else if (strcmp(str,"NEIGHBORS")==0)
{
scanf("%d",&m);
neighbor(m);
}
}
}
return 0;
}
相关文章推荐
- HDU 5867 Water problem(模拟题目)【HDU多校联合8.18】
- 语言入门-题目33(蛇形矩阵)_模拟
- 【noip题目代码】海港;模拟,iterator,memset(TLE)
- 2016.08.19上午【初中部 NOIP普及组 】模拟赛题目
- 腾讯2016-9-1 软件测试工程师 模拟考 第二部分 程序题目
- HDOJ 题目3456 Universal Oracle(模拟,水)
- NYOJ 题目93汉诺塔(三)(模拟)
- 模拟_题目1079:手机键盘
- HDOJ 题目5444 Elven Postman(BST模拟水啊。。。)
- HDOJ 题目1302 The Snail(模拟,水题)
- 会计电算化练习题目三(模拟练习8)---2014年会计从业资格考试办法将有重大调整
- 2016-08-20-java-多线程的一个题目:用线程模拟竞拍过程
- 2017校招全国统一模拟考试(第五场)JavaScript解法记录(题目来源-->牛客网)
- 【noip题目代码】回文日期;vetor,模拟,iterator
- HDU 2206 IP的计算(模拟题目)
- 第十四天:poj1016(模拟,看懂题目最重要!!!)
- SDUT2156 Fairy tale(第一届省赛题目)(模拟)
- HDOJ 题目5097 Page Rank(矩阵运算,模拟)
- NYOJ 题目46最少乘法次数(模拟)
- HDOJ 题目1276士兵队列训练问题(队列,模拟)