题目出处:点击打开链接
8 numbers problem |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB |
Total submit users: 107, Accepted users: 93 |
Problem 11443 : No special judgement |
Problem description |
I think almost every acmer will know the 8 numbers problem which is a very famous problem.The game begin from the initial state of a 3*3 matrix which makes up of 8 numbers(1-8) and a blank block(0).move the blank block with its adjacent block until reach the objective state.It is obvious that the blank block has four directions which it can move to when it is at the middle position,i.e.up,down,left,right.Also,it has two directions when it is at the corner of the matrix and three directions at other posintion. Form example,the initial state of the matrix: 8 0 3 2 1 4 7 6 5the objective state: 1 2 3 8 0 4 7 6 5and we give a valid moving path: 8 0 3 8 1 3 8 1 3 0 1 3 1 0 3 1 2 3 2 1 4 > 2 0 4 > 0 2 4 > 8 2 4 > 8 2 4 > 8 0 4 7 6 5 7 6 5 7 6 5 7 6 5 7 6 5 7 6 5Moreover,the path with least steps is called the shortest path.And the 8 numbers is check whether there are the path from the initial state to the objective state and if it exists,give the shortest path. And we all know huicpc229 is not very good at search,so he hasn't solved this problem now.But he has solved another easy problem. The problem is described as follow: Give an initial state of the matrix,and give a sequence of moving. For every moving,if the blank block can move to the direction as the moving,move it,otherwise ignore this moving.And we want to know the final state of the matrix. |
Input |
The first line of the input is one integer t,the number of testcase. For each testcase: Three lines respond to the initial state of the matrix,and there will be three numbers on each of the three lines. Follow by an interger m corresponding to the number of moving. The next m line,every line contain only one character: U: move the blank block up for one block. D: move the blank block down for one block. L: move the blank block left for one block. R: move the blank block right for one block. |
Output |
For each testcase output the final state of the matrix for three lines as above.And there will be a blank space between every two numbers on the same line.And you should output one blank line after each testcase. |
Sample Input |
1 8 0 3 2 1 4 7 6 5 2 D R |
Sample Output |
8 1 3 2 4 0 7 6 5 |
Problem Source |
yilinghello@footmen |
水题,注意边界的0是不能往外移动的,加上判定条件
每组数据之间一行空格
the accepted code like this:
//
// main.cpp
// J
//
// Created by IntelliegeWither on 16/1/29.
// Copyright © 2016年 IntelliegeWither. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a[10][10];
int i,j;
int signi=0,signj=0;
int m;
char ch;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==0)
{
signi=i;signj=j;
}
}
}
scanf("%d",&m);
getchar();
for(i=0;i<m;i++)
{
scanf("%c",&ch);
getchar();
// int temp;
if(ch=='U')
{
if(signi>1)
{
//temp=a[signi][signj];
a[signi][signj]=a[signi-1][signj];
//a[signi-1][signj]=temp;
signi-=1;
}
}
else if(ch=='D')
{
if(signi<3)
{
// temp=a[signi][signj];
a[signi][signj]=a[signi+1][signj];
//a[signi+1][signj]=temp;
signi+=1;
}
}
else if(ch=='L')
{
if(signj>1)
{
//temp=a[signi][signj];
a[signi][signj]=a[signi][signj-1];
//a[signi][signj-1]=temp;
signj-=1;
}
}
else if(ch=='R')
{
if(signj<3)
{
//temp=a[signi][signj];
a[signi][signj]=a[signi][signj+1];
// a[signi][signj+1]=temp;
signj+=1;
}
}
if(i==m-1)
{
a[signi][signj]=0;
}
}
for(i=1;i<=3;i++)
{
for(j=1;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("%d\n",a[i][j]);
}
if(t!=0)
{
printf("\n");
}
}
}