hdu 2598 Manipulating the Power Square 模拟水题

Manipulating the Power Square

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 186    Accepted Submission(s): 62


Problem Description
Rosalina finds Super Mario puzzling over the Power Square and gives him another hint on how to unlock its power. ”Keep swapping 0 with one of its neighbors! I’ll tell you which neighbor,” Rosalina says to Mario. Rosalina then gives Mario a sequence of directions on which neighbor to swap with 0.
 

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 ”MOVE” is followed by one more more moves: ”up”, ”down”, ”left”, or ”right”.
Each move is executed as follows:
– For ”up”, swap 0 with its neighbor above.
– For ”left”, swap 0 with its left neighbor.
– For ”right”, swap 0 with its right neighbor.
– For ”down”, swap 0 with its neighbor below.
If move attempts to swap 0 with a non-existent neighbor, then output ”FAILED” on a single line and stop attempting the remaining moves. If the move succeeds, ”MOVED” is output on a single line. In either case, the state of the Power Square is changed to reflect the moves made.
 

Sample Input
  
  
3 8 7 6 5 4 3 2 1 0 SHOW MOVE up SHOW 3 8 7 6 5 4 3 2 1 0 SHOW MOVE up right
 

Sample Output
  
  
8 7 6 5 4 3 2 1 0 MOVED 8 7 6 5 4 0 2 1 3 8 7 6 5 4 3 2 1 0 FAILED
 

Source
 

Recommend
lcy
 
题意:
输入  n  以及MOVE   SHOW 等命令 输入n  则之后要输入一个n*n的矩阵    输入SHOW 要输出矩阵    输入MOVE  其后可以跟多个子命令(up down right left)  表示将0和其上下右左
进行交换 如果MOVE后的所有操作都可以完成 则输出MOVED  否则输出FAILED        
 
思路:  直接模拟
#include<stdio.h>
#include<string.h>
char s[10000];
int map[111][111];
int main()
{
     int n,i,j,flag=0,x,y,xx,yy,cg;//cg表示交换是否成功  flag表示输入是否依旧是up right down left 
     while(scanf("%s",s)!=EOF)
     {
         if(strcmp(s,"MOVE")==0)
         {
              if(flag==1)
              {
                 if(cg==1) printf("MOVED\n");
                 else printf("FAILED\n");
                 cg=1;
                  flag=0;
              }
         }
         else if(strcmp(s,"SHOW")==0)
         {

                if(flag==1)
                {
                        if(cg==1) printf("MOVED\n");
                            else printf("FAILED\n");
                         cg=1;
                         flag=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,"up")==0)
         {
             if(cg==0) continue;
                 flag=1;
                 xx=x-1;yy=y;
                 if(xx<1||xx>n||yy<1||yy>n) cg=0;
                 else
                    {
                        int temp;
                        temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp;
                         //temp=x; x=xx;xx=temp; temp=y; y=yy;yy=temp;
                         x=xx;y=yy;
                    }
         }
        else if(strcmp(s,"left")==0)
         {
                 if(cg==0) continue;
                 flag=1;
                 xx=x;yy=y-1;
                 if(xx<1||xx>n||yy<1||yy>n) cg=0;
                 else
                    {
                        int temp;
                        temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp;
                         //temp=x; x=xx;xx=temp; temp=y; y=yy;yy=temp;
                         x=xx;y=yy;
                    }
         }
        else if(strcmp(s,"right")==0)
        {
                 if(cg==0) continue;
                 flag=1;
                 xx=x;yy=y+1;
                 if(xx<1||xx>n||yy<1||yy>n) cg=0;
                 else
                    {
                        int temp;
                        temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp;
                         //temp=x; x=xx;xx=temp; temp=y; y=yy;yy=temp;
                         x=xx;y=yy;
                    }
        }
        else if(strcmp(s,"down")==0)
        {
                 if(cg==0) continue;
                 flag=1;
                 xx=x+1;yy=y;
                 if(xx<1||xx>n||yy<1||yy>n) cg=0;
                 else
                    {
                        int temp;
                        temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp;
                         //temp=x; x=xx;xx=temp; temp=y; y=yy;yy=temp;
                         x=xx;y=yy;
                    }
        }
        else
            {
              if(flag==1)
              {
                 if(cg==1) printf("MOVED\n");
                 else printf("FAILED\n");
              }
                flag=0;
                n=0;cg=1;
                //printf("s=%s\n",s);
                for(i=0;s[i]!='\0';i++) n=n*10+s[i]-'0';
               // printf("n=%d\n",n);
                for(i=1;i<=n;i++)
                    for(j=1;j<=n;j++)
                      {
                         scanf("%d",&map[i][j]);
                         if(map[i][j]==0) {x=i;y=j;}
                      }
            }
     }
     if(flag==1)//如果最后一个命令是MOVE  要在所有输入结束后输出下面的结果
              {
                 if(cg==1) printf("MOVED\n");
                 else printf("FAILED\n");
              }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值