Page 1 of 2

10443 - Rock, Scissors, Paper

Posted: Mon Feb 03, 2003 9:27 am
by Dominik Michniewski
This problm looks simple ... but I got WA. Can anyone tell me, If I made a mistake in my algorthm ?

Code: Select all

cutted..
I found my mistake I got Acc.But is any other method to solve this question instead of simulating ?

Dominik

Time out

Posted: Sat Feb 08, 2003 12:50 am
by Eric3k
Hi,
Could any one please tell me why my code is timing out. I'm doing a simulation and I believe the code should work. However, it's timing out in the online judge.
Thanks

[cpp]
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;
bool A_beats_B(char a, char b){
return a=='R'&&b=='S' || a=='S'&&b=='P'||a=='P'&&b=='R';
}

void main(){
//ifstream in("in.txt");
int cases;
cin>>cases;
for (int i=0;i<cases;i++){
int r,c,n;
cin>>r>>c>>n;
vector<string>grid;
string line;
for (int ro=0;ro<r;ro++){
cin>>line;
grid.push_back(line);
}
vector<string>grid1=grid;
for (int day=0;day<n;day++){
for (int i=0;i<r;i++){
for (int j=0;j<c;j++){
if (j<c-1)
if (A_beats_B(grid[j],grid[j+1]))grid1[j+1]=grid[j];
else grid1[j]=grid[j+1];
if (i<r-1)
if (A_beats_B(grid[j],grid[i+1][j]))grid1[i+1][j]=grid[j];
else grid1[j]=grid[i+1][j];
}
}
grid=grid1;

}
for (int o=0;o<grid1.size();o++)cout<<grid1[o]<<endl;


cout<<endl;
}
}

[/cpp]
[/cpp]

RSP

Posted: Sun Feb 09, 2003 2:44 am
by RuiFerreira
I sugest you to check who is ataking each position, instead to check who each position is ataking...

10443

Posted: Mon Mar 24, 2003 6:12 pm
by Nick
Hello,
i wonder why i got WA...... Does anyone know some tricky inputs/outputs?Here's my input and output :
INPUT :
10
0 0 3

1 1 3
R
1 10 4
SRPSRPPRSR
10 1 4
R
S
P
P
S
R
R
S
P
P
3 3 1
RRR
RSR
RRR
3 3 1000
RRR
RSR
RRR
3 4 3
RSPR
SPRS
PRSP
3 4 2
RSPR
SPRS
PRSP
3 4 3
PRSP
RSPR
SPRS
5 5 5
RRRSP
RSRSP
RRRRP
SRRRP
RPPRP

OUTPUT :

[a blank line]

R

RPPPPPPPPP

R
R
R
R
R
R
R
R
R
R

RRR
RRR
RRR

RRR
RRR
RRR

RRRR
RRRS
RRSP

RRRS
RRSP
RSPR

PPPP
PPPR
PPRS

PPPSP
PPSRS
PSRPP
PPSRP
PPPSR


can anyone tell what's wrong??

Posted: Tue Mar 25, 2003 3:30 pm
by angga888
Your outputs are correct. :)

Good Luck!

10443: Rock, Scissors, Paper

Posted: Fri Sep 12, 2003 4:04 am
by yan4546
In the following case ,who will take up the grid where the 'S' is in, after 1 day's war? And Why???

_R_
RSP
_P_

Re: 10443: Rock, Scissors, Paper

Posted: Fri Sep 12, 2003 4:19 am
by gvcormac
yan4546 wrote:In the following case ,who will take up the grid where the 'S' is in, after 1 day's war? And Why???

_R_
RSP
_P_
Rock. What other possibility did you have in mind?

No other mind!

Posted: Fri Sep 12, 2003 4:39 am
by yan4546
No other mind!

Posted: Mon Sep 15, 2003 1:28 pm
by bery olivier
after one day :

_R_
RRS
_S_

Posted: Mon Oct 20, 2003 12:02 am
by Maarten
??? what do you mean ?
I tried to solve this problem (and in fact I did solve it), but my solution is rather slow (it runs in 3.6 seconds).
I am wondering if there is a faster way to solve the problem; what I did is just simulation

10443 WA

Posted: Sat Feb 26, 2005 8:56 am
by Zoe
hi!
I have tried some input in http://online-judge.uva.es/board/viewto ... ight=10443
My output is right.
I don't know why I got WA.

Code: Select all

cutted
Thanks!!

Posted: Sat Feb 26, 2005 7:37 pm
by Cruzer
You aren't taking into account sides and corners of the grid. You are accessing outside the array dimensions. For example, if there is a rock in the top left corner, you check up to see if there are scissors, you also check left...

Posted: Sun Feb 27, 2005 5:35 am
by Zoe
I changed my code and got AC(P.E)!
Thanks a lot!!!

10443 Runtime Error (SIGSEGV) & wrong answer

Posted: Sat Mar 05, 2005 12:48 am
by lonelyone

Code: Select all

#include<stdio.h>
#include<malloc.h>
#include<string.h>

int A_beats_B(char a, char b)
{ 
	return (a=='R'&&b=='S')||(a=='S'&&b=='P')||(a=='P'&&b=='R'); 
} 

main()
{ 
	int cases;
 	int i,j,r,c,n,day,num,copy; 
 	char **grid,**copy_grid;
	scanf("%d",&cases);
	
	for (num=0;num<cases;num++)
 	{ 	
	 	scanf("%d %d %d",&r,&c,&n); 
	 	getchar();
	 	grid=(char **)malloc(r*sizeof(char *));
	 	copy_grid=(char **)malloc(r*sizeof(char *));
	 	for(i=0;i<r;i++)
	 	{
	 		grid[i]=(char *)malloc(c*sizeof(char));
	 		copy_grid[i]=(char *)malloc(c*sizeof(char));
 		}	 		
	 	for (i=0;i<r;i++)
	 	{
   		    gets(grid[i]);
   		    strcpy(copy_grid[i],grid[i]);
   		}    
 		for (day=0;day<n;day++)
   		{ 
   			for (i=0;i<r;i++)
      		{ 
      			for (j=0;j<c;j++)
         		{ 
         			if (j>0) 
         			{
         				if (A_beats_B(copy_grid[i][j],copy_grid[i][j-1]))
             				grid[i][j-1]=copy_grid[i][j]; 
         				else 
             				grid[i][j]=copy_grid[i][j-1]; 
       				}	
       				if (i>0) 
       				{
       					if (A_beats_B(copy_grid[i][j],copy_grid[i-1][j]))
            				grid[i-1][j]=copy_grid[i][j]; 
       					else 
            				grid[i][j]=copy_grid[i-1][j]; 
					}
         			if (j<c-1) 
         			{
         				if (A_beats_B(copy_grid[i][j],copy_grid[i][j+1]))
             				grid[i][j+1]=copy_grid[i][j]; 
         				else 
             				grid[i][j]=copy_grid[i][j+1]; 
       				}				
       				if (i<r-1) 
       				{
       					if (A_beats_B(copy_grid[i][j],copy_grid[i+1][j]))
            				grid[i+1][j]=copy_grid[i][j]; 
       					else 
            				grid[i][j]=copy_grid[i+1][j]; 
					}
			    } 
		    } 
		    
		    for(copy=0;copy<r;copy++)
		    	strcpy(copy_grid[copy],grid[copy]); 
	    } 
	    for(i=0;i<r;i++)
	    	puts(grid[i]);
    	if(num!=cases-1)
    		printf("\n");
    } 
} 
could someone explain this problem to me...
i don't know how to solve it...
and why this code get runtime error...
thx a million

Posted: Sat Mar 05, 2005 7:33 pm
by Ryan Pai
For each string you're only allocating c characters. You need one more for the null terminator.

You also don't free the memory you malloc, so there's a leak.

I'd suggest using a static two dimensional array insead.