10443 - Rock

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

Moderator: Board moderators

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

10443 - Rock, Scissors, Paper

Post 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
Eric3k
New poster
Posts: 29
Joined: Mon Apr 29, 2002 5:22 pm

Time out

Post 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]
RuiFerreira
New poster
Posts: 23
Joined: Mon Dec 16, 2002 8:01 pm
Location: Portugal
Contact:

RSP

Post by RuiFerreira »

I sugest you to check who is ataking each position, instead to check who each position is ataking...
Please visit my webpage!! I've got a lot of UVA statistics scripts
http://www.fe.up.pt/~ei01081/scripts/
Nick
Learning poster
Posts: 53
Joined: Sun Jan 12, 2003 4:49 am

10443

Post 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??
angga888
Experienced poster
Posts: 143
Joined: Sat Dec 21, 2002 11:41 am
Location: Indonesia

Post by angga888 »

Your outputs are correct. :)

Good Luck!
yan4546
New poster
Posts: 7
Joined: Fri Nov 15, 2002 4:17 am
Contact:

10443: Rock, Scissors, Paper

Post 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_
gvcormac
Problemsetter & Reviewer
Posts: 194
Joined: Fri Mar 15, 2002 2:00 am
Contact:

Re: 10443: Rock, Scissors, Paper

Post 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?
yan4546
New poster
Posts: 7
Joined: Fri Nov 15, 2002 4:17 am
Contact:

No other mind!

Post by yan4546 »

No other mind!
aaaaaaaaaaaaaaaaaaaaa
bery olivier
Learning poster
Posts: 90
Joined: Sat Feb 15, 2003 1:39 am
Location: Paris, France
Contact:

Post by bery olivier »

after one day :

_R_
RRS
_S_
Not AC yet Image AC at last Image
Maarten
Experienced poster
Posts: 108
Joined: Sat Sep 27, 2003 5:24 pm

Post 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
Zoe
New poster
Posts: 9
Joined: Tue Jul 20, 2004 5:46 am
Location: Taiwen

10443 WA

Post 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!!
Last edited by Zoe on Sun Feb 27, 2005 5:33 am, edited 1 time in total.
Cruzer
New poster
Posts: 11
Joined: Thu Feb 10, 2005 4:18 am
Location: Waterloo, ON, Canada

Post 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...
Zoe
New poster
Posts: 9
Joined: Tue Jul 20, 2004 5:46 am
Location: Taiwen

Post by Zoe »

I changed my code and got AC(P.E)!
Thanks a lot!!!
lonelyone
Learning poster
Posts: 65
Joined: Sat Feb 19, 2005 6:53 pm

10443 Runtime Error (SIGSEGV) & wrong answer

Post 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
Ryan Pai
Learning poster
Posts: 67
Joined: Fri Jul 04, 2003 9:59 am
Location: USA

Post 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.
I'm always willing to help, if you do the same.
Post Reply

Return to “Volume 104 (10400-10499)”