Page 4 of 4

WA 227

Posted: Sat May 22, 2010 8:24 am
by scanf
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;
}

Re: WA 227

Posted: Wed May 26, 2010 11:35 am
by sohel
Check out the discussions here
Don't create a new thread for a problem that already exists.

Re: 227 puzzle

Posted: Sat Jun 02, 2012 12:00 pm
by novice2
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

Re: 227 puzzle

Posted: Sun Dec 23, 2012 11:32 pm
by brianfry713
input:

Code: Select all

TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBLQ0
Z
Correct output:

Code: Select all

Puzzle #1:
This puzzle has no final configuration.

Re: 227 puzzle

Posted: Thu May 01, 2014 1:07 pm
by pivko
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:

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();
	}

}

Re: 227 puzzle

Posted: Thu May 01, 2014 9:06 pm
by brianfry713
Separate output from different puzzle records by one blank line. Don't print an extra blank line at the end.

Re: 227 puzzle

Posted: Thu May 01, 2014 10:59 pm
by pivko
It was blank line at the and :roll: Many thanks for the help :) It's ridiculous that I've got stuck on this for hours :oops:

I get WA. Need help

Posted: Tue Jun 24, 2014 4:08 pm
by lighted
I checked input tests in this thread but got WA. Can someone give me some tricky input/output tests?

Code: Select all

removed, after acc..
Thank you very much brianfry713!
I often have problems with reading input.

Code: Select all

for (ok = 1; (c = getchar()) != '0'; ) {
    ...       
}

while(getchar() != '\n');
I also changed my old code and got acc.

Code: Select all

for (ok = 1; scanf(" %c", &c) == 1; ) {

  if (c == '0') break;
    ...       
}

while(getchar() != '\n');

Re: 227 puzzle

Posted: Tue Jun 24, 2014 8:29 pm
by brianfry713
PM sent

Re: 227 - Puzzle

Posted: Thu Nov 06, 2014 7:52 am
by Nahian.Sunny
Removed after AC :D

Re: 227 - Puzzle

Posted: Thu Nov 06, 2014 1:20 pm
by lighted
brianfry713 wrote:Separate output from different puzzle records by one blank line. Don't print an extra blank line at the end.

Re: 227 - Puzzle

Posted: Fri Sep 23, 2016 9:05 pm
by mgavin2

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
make sure to read input line by line. it's imperative.