程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 2599 The Dreadful Seven 模擬水題

hdu 2599 The Dreadful Seven 模擬水題

編輯:C++入門知識

The Dreadful Seven
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 265    Accepted Submission(s): 101

 

Problem Description
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 right of 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
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 the top 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.
 


Output
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 values neighboring 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.
 


Sample Input
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


Sample Output
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


Source
HDU 2010-05 Programming Contest
 


Recommend
lcy
 
題意:  輸入n  再輸入n*n的矩陣    輸入未知個數的命令
輸入SHOW 則輸出矩陣  輸入  NEIGHBORS m  輸出m  上左右下的數字  
 
思路:
本題不難 主要是對於命令的個數未知 我們可以用 字符串輸入 判定3種輸入的格式 如果不是那2種命令 則輸入的是n
具體看代碼
by hnust_xiehonghao
 
[cpp]
#include<stdio.h>  
#include<string.h>  
int map[111][111]; 
int main() 

    int n,i,j,m; 
    char s[100]; 
    while(scanf("%s",s)!=EOF) 
    { 
        if(strcmp(s,"SHOW")==0) 
        { 
            for(i=1;i<=n;i++) 
            { 
                for(j=1;j<n;j++) 
                    printf("%d ",map[i][j]); 
                printf("%d\n",map[i][j]); 
            } 
            printf("\n"); 
        } 
        else  if(strcmp(s,"NEIGHBORS")==0) 
        { 
            scanf("%d",&m); 
            int flag=1; 
            for(i=1;i<=n;i++) 
            { 
                for(j=1;j<=n;j++) 
                    if(map[i][j]==m)  
                    {flag=0;break;} 
                if(flag==0) break; 
 
            } 
                    int out[5],k,cnt=0; 
                    if(map[i-1][j]!=-1) out[cnt++]=map[i-1][j]; 
                    if(map[i][j-1]!=-1) out[cnt++]=map[i][j-1]; 
                    if(map[i][j+1]!=-1) out[cnt++]=map[i][j+1]; 
                    if(map[i+1][j]!=-1) out[cnt++]=map[i+1][j]; 
                    for(k=0;k<cnt-1;k++)  printf("%d ",out[k]); 
                    printf("%d\n",out[cnt-1]); 
        } 
        else 
        { 
            int k=0; 
            n=0; 
            while(s[k]!='\0') 
            { 
                n=n*10+(s[k]-'0'); 
                k++; 
            } 
            memset(map,-1,sizeof(map)); 
            for(i=1;i<=n;i++) 
                for(j=1;j<=n;j++) 
                    scanf("%d",&map[i][j]);    
        } 
    } 
    return 0; 

#include<stdio.h>
#include<string.h>
int map[111][111];
int main()
{
 int n,i,j,m;
 char s[100];
 while(scanf("%s",s)!=EOF)
 {
  if(strcmp(s,"SHOW")==0)
  {
   for(i=1;i<=n;i++)
   {
    for(j=1;j<n;j++)
     printf("%d ",map[i][j]);
    printf("%d\n",map[i][j]);
   }
   printf("\n");
  }
  else  if(strcmp(s,"NEIGHBORS")==0)
  {
   scanf("%d",&m);
   int flag=1;
   for(i=1;i<=n;i++)
   {
    for(j=1;j<=n;j++)
     if(map[i][j]==m)
     {flag=0;break;}
    if(flag==0) break;

   }
     int out[5],k,cnt=0;
     if(map[i-1][j]!=-1) out[cnt++]=map[i-1][j];
     if(map[i][j-1]!=-1) out[cnt++]=map[i][j-1];
     if(map[i][j+1]!=-1) out[cnt++]=map[i][j+1];
     if(map[i+1][j]!=-1) out[cnt++]=map[i+1][j];
     for(k=0;k<cnt-1;k++)  printf("%d ",out[k]);
     printf("%d\n",out[cnt-1]);
  }
  else
  {
   int k=0;
   n=0;
   while(s[k]!='\0')
   {
    n=n*10+(s[k]-'0');
    k++;
   }
   memset(map,-1,sizeof(map));
   for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
     scanf("%d",&map[i][j]);  
  }
 }
 return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved