227 - Puzzle

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

scanf
New poster
Posts: 1
Joined: Sat May 22, 2010 8:13 am

WA 227

Post by scanf » Sat May 22, 2010 8:24 am

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

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Re: WA 227

Post by sohel » Wed May 26, 2010 11:35 am

Check out the discussions here
Don't create a new thread for a problem that already exists.

novice2
New poster
Posts: 3
Joined: Tue May 08, 2012 6:30 pm

Re: 227 puzzle

Post by novice2 » Sat Jun 02, 2012 12:00 pm

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

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 227 puzzle

Post by brianfry713 » Sun Dec 23, 2012 11:32 pm

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.
Check input and AC output for thousands of problems on uDebug!

pivko
New poster
Posts: 2
Joined: Thu May 01, 2014 12:10 pm

Re: 227 puzzle

Post by pivko » Thu May 01, 2014 1:07 pm

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

}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 227 puzzle

Post by brianfry713 » Thu May 01, 2014 9:06 pm

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!

pivko
New poster
Posts: 2
Joined: Thu May 01, 2014 12:10 pm

Re: 227 puzzle

Post by pivko » Thu May 01, 2014 10:59 pm

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:

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

I get WA. Need help

Post by lighted » Tue Jun 24, 2014 4:08 pm

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');
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

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 227 puzzle

Post by brianfry713 » Tue Jun 24, 2014 8:29 pm

PM sent
Check input and AC output for thousands of problems on uDebug!

Nahian.Sunny
New poster
Posts: 6
Joined: Tue Oct 21, 2014 12:51 pm

Re: 227 - Puzzle

Post by Nahian.Sunny » Thu Nov 06, 2014 7:52 am

Removed after AC :D
Last edited by Nahian.Sunny on Fri Nov 07, 2014 6:24 am, edited 1 time in total.

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 227 - Puzzle

Post by lighted » Thu Nov 06, 2014 1:20 pm

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

mgavin2
New poster
Posts: 43
Joined: Sat Jul 28, 2012 6:29 pm

Re: 227 - Puzzle

Post by mgavin2 » Fri Sep 23, 2016 9:05 pm

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.
all that matters is AC

Post Reply

Return to “Volume 2 (200-299)”