227 - Puzzle
Moderator: Board moderators
WA 227
plz help me. i get WA.. WA.. WA.. WA.. WA.. WA..
#include<stdio.h>
#include<string.h>
main()
{
char sa[30][30],order[500],ch;
long row,col,i,j,k,flag=0,kase=1;
while(1)
{
flag=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf("%c",&sa[j]);
if(sa[0][0]=='Z')
{
flag=1;
break;
}
if(sa[j]==' ')
{
row=i;
col=j;
}
}
if(flag==1)
break;
scanf("%c",&ch);
}
if(flag==1)
break;
k=0;
while(scanf("%c",&ch)!=EOF)
{
if(ch=='0')
break;
if(ch=='A'||ch=='B'||ch=='R'||ch=='L')
order[k++]=ch;
if(ch=='a'||ch=='b'||ch=='r'||ch=='l')
flag=1;
}
printf("Puzzle #%ld:\n",kase++);
if(flag==0)
{
for(i=0;i<k;i++)
{
if(order=='A')
{
if((sa[row-1][col]>='A'&&sa[row-1][col]<='Z')||(sa[row-1][col]>='a'&&sa[row-1][col]<='z'))
{
sa[row][col]=sa[row-1][col];
row--;
sa[row][col]=' ';
}
else
{
flag=1;
break;
}
}
else if(order=='B')
{
if((sa[row+1][col]>='A'&&sa[row+1][col]<='Z')||(sa[row+1][col]>='a'&&sa[row+1][col]<='z'))
{
sa[row][col]=sa[row+1][col];
row++;
sa[row][col]=' ';
}
else
{
flag=1;
break;
}
}
else if(order=='R')
{
if((sa[row][col+1]>='A'&&sa[row][col+1]<='Z')||(sa[row][col+1]>='a'&&sa[row][col+1]<='z'))
{
sa[row][col]=sa[row][col+1];
col++;
sa[row][col]=' ';
}
else
{
flag=1;
break;
}
}
else if(order=='L')
{
if((sa[row][col-1]>='A'&&sa[row][col-1]<='Z')||(sa[row][col-1]>='a'&&sa[row][col-1]<='z'))
{
sa[row][col]=sa[row][col-1];
col--;
sa[row][col]=' ';
}
else
{
flag=1;
break;
}
}
}
}
if(flag==0)
{
for(i=0;i<5;i++)
{ for(j=0;j<5;j++)
{
if(j==0)
printf("%c",sa[j]);
else
printf(" %c",sa[j]);
}
printf("\n");
}
}
else
printf("This puzzle has no final configuration.\n");
memset(sa,0,sizeof(sa));
memset(order,0,sizeof(order));
printf("\n");
scanf("%c",&ch);
}
return 0;
}
#include<stdio.h>
#include<string.h>
main()
{
char sa[30][30],order[500],ch;
long row,col,i,j,k,flag=0,kase=1;
while(1)
{
flag=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf("%c",&sa[j]);
if(sa[0][0]=='Z')
{
flag=1;
break;
}
if(sa[j]==' ')
{
row=i;
col=j;
}
}
if(flag==1)
break;
scanf("%c",&ch);
}
if(flag==1)
break;
k=0;
while(scanf("%c",&ch)!=EOF)
{
if(ch=='0')
break;
if(ch=='A'||ch=='B'||ch=='R'||ch=='L')
order[k++]=ch;
if(ch=='a'||ch=='b'||ch=='r'||ch=='l')
flag=1;
}
printf("Puzzle #%ld:\n",kase++);
if(flag==0)
{
for(i=0;i<k;i++)
{
if(order=='A')
{
if((sa[row-1][col]>='A'&&sa[row-1][col]<='Z')||(sa[row-1][col]>='a'&&sa[row-1][col]<='z'))
{
sa[row][col]=sa[row-1][col];
row--;
sa[row][col]=' ';
}
else
{
flag=1;
break;
}
}
else if(order=='B')
{
if((sa[row+1][col]>='A'&&sa[row+1][col]<='Z')||(sa[row+1][col]>='a'&&sa[row+1][col]<='z'))
{
sa[row][col]=sa[row+1][col];
row++;
sa[row][col]=' ';
}
else
{
flag=1;
break;
}
}
else if(order=='R')
{
if((sa[row][col+1]>='A'&&sa[row][col+1]<='Z')||(sa[row][col+1]>='a'&&sa[row][col+1]<='z'))
{
sa[row][col]=sa[row][col+1];
col++;
sa[row][col]=' ';
}
else
{
flag=1;
break;
}
}
else if(order=='L')
{
if((sa[row][col-1]>='A'&&sa[row][col-1]<='Z')||(sa[row][col-1]>='a'&&sa[row][col-1]<='z'))
{
sa[row][col]=sa[row][col-1];
col--;
sa[row][col]=' ';
}
else
{
flag=1;
break;
}
}
}
}
if(flag==0)
{
for(i=0;i<5;i++)
{ for(j=0;j<5;j++)
{
if(j==0)
printf("%c",sa[j]);
else
printf(" %c",sa[j]);
}
printf("\n");
}
}
else
printf("This puzzle has no final configuration.\n");
memset(sa,0,sizeof(sa));
memset(order,0,sizeof(order));
printf("\n");
scanf("%c",&ch);
}
return 0;
}
Re: WA 227
Check out the discussions here
Don't create a new thread for a problem that already exists.
Don't create a new thread for a problem that already exists.
Re: 227 puzzle
the sequence of moves is a long string and contains spaces
cin>>str terminates on whitespaces
&
getline(cin,str) cant accept long string
gets(str) serves both
cin>>str terminates on whitespaces
&
getline(cin,str) cant accept long string
gets(str) serves both
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 227 puzzle
input:Correct output:
Code: Select all
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBLQ0
Z
Code: Select all
Puzzle #1:
This puzzle has no final configuration.
Check input and AC output for thousands of problems on uDebug!
Re: 227 puzzle
Hi,
I'm still getting WA. However, my code works for all test cases I've tried. Any tips what to fix?
Here is my java code:
I'm still getting WA. However, my code works for all test cases I've tried. Any tips what to fix?
Here is my java code:
Code: Select all
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/*************************************
* ACM 227 Puzzle
*
* Nacte puzzle a provede zadane tahy.
*
* @author David Pivovar A13B0403P
*
*/
public class Main {
// cislo puzzli
private static int puzzleNo = 0;
// puzzle
private static char[][] puzzle;
// fronta pohybu
private static Queue<Character> moves;
// souradnice volneho mista
private static int iSpace;
private static int jSpace;
// chyba se nastavi na true, pokud doslo k nepovolenemu presunu
private static boolean chyba;
// scanner
private static Scanner sc = new Scanner(System.in);
/**
* @param args
*/
public static void main(String[] args) {
String firstLine;
while ((firstLine = sc.nextLine()).length() > 1) {
puzzleNo++;
init(firstLine);
move();
print();
}
}
/*************************************************
* Inicializuje puzzle a frontu pohybu
*
* @param line
* prvni radka vstupnich puzzli
*/
private static void init(String line) {
puzzle = new char[5][5];
moves = new LinkedList<Character>();
chyba = false;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
puzzle[i][j] = line.charAt(j);
if (line.charAt(j) == ' ') {
iSpace = i;
jSpace = j;
}
}
line = sc.nextLine();
}
while (!moves.contains('0')){
for (int i = 0; i < line.length(); i++) {
moves.add(line.charAt(i));
}
if (!moves.contains('0')) {
line = sc.nextLine();
}
}
}
/*******************************************************
* Provede jeden tah.
*
* Rekurzivne se vola tak, aby se provedly vsechny tahy.
*/
private static void move() {
switch (moves.poll()) {
case 'A':
if (iSpace >= 1) {
puzzle[iSpace][jSpace] = puzzle[iSpace - 1][jSpace];
puzzle[iSpace - 1][jSpace] = ' ';
iSpace--;
move();
} else {
chyba = true;
}
break;
case 'B':
if (iSpace <= 3) {
puzzle[iSpace][jSpace] = puzzle[iSpace + 1][jSpace];
puzzle[iSpace + 1][jSpace] = ' ';
iSpace++;
move();
} else {
chyba = true;
}
break;
case 'L':
if (jSpace >= 1) {
puzzle[iSpace][jSpace] = puzzle[iSpace][jSpace - 1];
puzzle[iSpace][jSpace - 1] = ' ';
jSpace--;
move();
} else {
chyba = true;
}
break;
case 'R':
if (jSpace <= 3) {
puzzle[iSpace][jSpace] = puzzle[iSpace][jSpace + 1];
puzzle[iSpace][jSpace + 1] = ' ';
jSpace++;
move();
} else {
chyba = true;
}
break;
case '0':
break;
default:
chyba = true;
break;
}
//if (!moves.isEmpty())
// move();
}
/***********************************
* Vypise puzzle na obrazovku.
*/
private static void print() {
System.out.println("Puzzle #" + puzzleNo + ":");
if (!chyba) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(puzzle[i][j]);
if(j!=4)
System.out.print(" ");
}
System.out.println();
}
} else {
System.out.println("This puzzle has no final configuration.");
}
System.out.println();
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 227 puzzle
Separate output from different puzzle records by one blank line. Don't print an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!
Re: 227 puzzle
It was blank line at the and
Many thanks for the help
It's ridiculous that I've got stuck on this for hours ![:oops:](./images/smilies/icon_redface.gif)
![:roll:](./images/smilies/icon_rolleyes.gif)
![:)](./images/smilies/icon_smile.gif)
![:oops:](./images/smilies/icon_redface.gif)
I get WA. Need help
I checked input tests in this thread but got WA. Can someone give me some tricky input/output tests?
Thank you very much brianfry713!
I often have problems with reading input.
I also changed my old code and got acc.
Code: Select all
removed, after acc..
I often have problems with reading input.
Code: Select all
for (ok = 1; (c = getchar()) != '0'; ) {
...
}
while(getchar() != '\n');
Code: Select all
for (ok = 1; scanf(" %c", &c) == 1; ) {
if (c == '0') break;
...
}
while(getchar() != '\n');
Last edited by lighted on Wed Jun 25, 2014 9:52 am, edited 2 times in total.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
-
- New poster
- Posts: 6
- Joined: Tue Oct 21, 2014 12:51 pm
Re: 227 - Puzzle
Removed after AC ![:D](./images/smilies/icon_biggrin.gif)
![:D](./images/smilies/icon_biggrin.gif)
Last edited by Nahian.Sunny on Fri Nov 07, 2014 6:24 am, edited 1 time in total.
Re: 227 - Puzzle
brianfry713 wrote:Separate output from different puzzle records by one blank line. Don't print an extra blank line at the end.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Re: 227 - Puzzle
Code: Select all
I won.
The ending condition is for sure:
fgets(line, some number, stdin);
if (line[0] == 'Z' && strlen(line) == 2) stop processing
all that matters is AC