340 - Master-Mind Hints

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

Moderator: Board moderators

arc16
Learning poster
Posts: 62
Joined: Sun Aug 04, 2002 1:05 am
Location: Indonesia

340 - Master-Mind Hints

Post by arc16 » Sun Aug 18, 2002 3:47 pm

the problem is simple, but i got WA
is there any tricky case?

arc16
Learning poster
Posts: 62
Joined: Sun Aug 04, 2002 1:05 am
Location: Indonesia

Post by arc16 » Sat Aug 31, 2002 3:46 am

my stupid mistake :P
somehow i set 100 for the max size where it should be 1000 :D

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one » Mon Dec 23, 2002 7:47 pm

I know it simple , but WA keeping me still.

Btw, where should I print "game #%d" , is it before the input or after the input??

epsilon0
Experienced poster
Posts: 112
Joined: Tue Nov 12, 2002 11:15 pm
Location: Paris, France.

Post by epsilon0 » Sun Dec 29, 2002 2:27 pm

deddy one it doesn't matter wether you print stuff before or after you read the input!!!
what matters is what you output... you could read all the input at once in a large buffer, or one job at a time, as you like.

this problem is not hard...think about special cases..

things like

123456789
111111111
We never perform a computation ourselves, we just hitch a ride on the great Computation that is going on already. --Tomasso Toffoli

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one » Mon Dec 30, 2002 8:01 pm

my output is


(1,0)



is it correct??
is there any other trick related to this problem?

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one » Tue Dec 31, 2002 2:49 am

thx epsilon, your test case inspired me of another test case
which is


987654321
111111111


this is the test case I needed to find my little bug

ericpo
New poster
Posts: 1
Joined: Mon May 26, 2003 6:48 am
Contact:

340--help!!

Post by ericpo » Mon May 26, 2003 6:52 am

I keep get WA,so is there anyone can give me some test data??
PLEASE....><

chfgress
New poster
Posts: 3
Joined: Tue Jun 03, 2003 2:55 am

Post by chfgress » Wed Jun 04, 2003 11:10 pm

I'm also having problems with WA, I tried all the input samples and some that I found in this forum... and a lot more... and it was always OK.

Do you have more input samples?

When you have a good reply (I know that i didn't help you, but), could you let me know?

Thx,
Carlos

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

340 why WA?

Post by chunyi81 » Mon Jul 07, 2003 6:13 am

[java]import java.io.*;
import java.util.*;
class Main {

static String ReadLn (int maxLg)
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}

catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null);
return (new String (lin, 0, lg));
}

public static void main(String[] args) {

int games = 1;
String line = ReadLn(255);

while(line.charAt(0) != '0') {

System.out.println("Game " + games + ":");

StringTokenizer st = new StringTokenizer(line);
int len = Integer.parseInt(st.nextToken());
int[] secret = new int[len];
int[] guess = new int[len];

st = new StringTokenizer(ReadLn(255));

for (int i = 0;i < len;i++)
secret = Integer.parseInt(st.nextToken());

st = new StringTokenizer(ReadLn(255));

for (int i = 0;i < len;i++)
guess = Integer.parseInt(st.nextToken());

while(guess[0] != 0) {

int strong = 0;
int weak = 0;
boolean[] match1 = new boolean[len];
boolean[] match2 = new boolean[len];

for (int i = 0;i < len;i++) {

if (guess == secret) {
match1 = true;
match2 = true;
strong++;
}

}

for (int j = 0;j < len;j++) {

if (!match2[j]) {

for (int k = 0;k < len;k++) {

if (!match1[k] && (guess[j] == secret[k])) {
match1[k] = true;
match2[j] = true;
weak++;
break;
}

}

}

}

System.out.println(" (" + strong + "," + weak + ")");

st = new StringTokenizer(ReadLn(255));

for (int i = 0;i < len;i++)
guess = Integer.parseInt(st.nextToken());

}

games++;

line = ReadLn(255);

}

}

}[/java]

Why the above code gives WA? It works with the special cases mentioned in this forum, and also works with the sample input.

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Re: 340 why WA?

Post by Observer » Mon Jul 07, 2003 6:36 am

I know nothing about Java...

But why "ReadLn(255);"??

Note that "The maximum value for N will be 1000"!!!

So the line may contain more than 255 characters!!
Last edited by Observer on Mon Jul 07, 2003 10:57 am, edited 1 time in total.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Re: 340 why WA?

Post by chunyi81 » Mon Jul 07, 2003 10:32 am

Observer wrote:I know nothing about Java...

But why "ReadLn(255);"??

Note that "The maximum value for N will be 1000"!!!
chunyi81 wrote:[java]import java.io.*;
import java.util.*;
class Main {

static String ReadLn (int maxLg)
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}

catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null);
return (new String (lin, 0, lg));
}

public static void main(String[] args) {

int games = 1;
String line = ReadLn(255);

while(line.charAt(0) != '0') {

System.out.println("Game " + games + ":");

StringTokenizer st = new StringTokenizer(line);
int len = Integer.parseInt(st.nextToken());
int[] secret = new int[len];
int[] guess = new int[len];

st = new StringTokenizer(ReadLn(255));

for (int i = 0;i < len;i++)
secret = Integer.parseInt(st.nextToken());

st = new StringTokenizer(ReadLn(255));

for (int i = 0;i < len;i++)
guess = Integer.parseInt(st.nextToken());

while(guess[0] != 0) {

int strong = 0;
int weak = 0;
boolean[] match1 = new boolean[len];
boolean[] match2 = new boolean[len];

for (int i = 0;i < len;i++) {

if (guess == secret) {
match1 = true;
match2 = true;
strong++;
}

}

for (int j = 0;j < len;j++) {

if (!match2[j]) {

for (int k = 0;k < len;k++) {

if (!match1[k] && (guess[j] == secret[k])) {
match1[k] = true;
match2[j] = true;
weak++;
break;
}

}

}

}

System.out.println(" (" + strong + "," + weak + ")");

st = new StringTokenizer(ReadLn(255));

for (int i = 0;i < len;i++)
guess = Integer.parseInt(st.nextToken());

}

games++;

line = ReadLn(255);

}

}

}[/java]

Why the above code gives WA? It works with the special cases mentioned in this forum, and also works with the sample input.


I know the max value of N is 1000. I used ReadLn(255) where ReadLn is a method defined to read from stdin(standard input) in Java. The number 255 means the max number of characters read per line.

Could u take a look at my code for problem 320 please? I could have misunderstood the problem 320. Thanks.

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

340 why WA

Post by chunyi81 » Mon Jul 07, 2003 11:53 am

Never mind, i found my mistake and got AC already. Some of the ReadLn statements I had to increase the max length of the string read to 2100 because the input for the code can be up to 1000(numbers in a code of length 1000) + 999(spaces) = 1999 characters. Thanks.

dt96hasv
New poster
Posts: 5
Joined: Wed Oct 20, 2004 9:27 am
Location: Gothenburg
Contact:

340 - Master-Mind hints

Post by dt96hasv » Wed Oct 20, 2004 9:45 am

Hi, I have been playing around with this problem for a while, and cannot find any example where my program is wrong, but still it is since the judge won't accept it ;)

[c]
#include <stdio.h>

int min(int a, int b){
if(a < b)
return a;
return b;
}

int main(){

int N,g=0,i,s,w;

char facit[1000];
char guess[1000];

int f_freq[10],g_freq[10];

while( scanf("%d",&N) == 1 && N > 0){
g++;
printf("Game %d:\n",g);

for(i = 0; i < N ; i++){
scanf("%d",&facit);
}

while(1){
for(i = 0; i < N ; i++){
scanf("%d",&guess);
}

if(guess[0] == 0) break;

s = w = 0;

[SNIP] Don't want to spoil all the fun ;) [/SNIP]

printf(" (%d,%d)\n",s,w);
}
}
}
[/c]
On an input file like

Code: Select all

4
1 3 5 5
1 1 2 3
4 3 3 5
6 5 5 1
6 1 3 5
1 3 5 5
0 0 0 0
10
1 2 2 2 4 5 6 6 6 9
1 2 3 4 5 6 7 8 9 1
1 1 2 2 3 3 4 4 5 5
1 2 1 3 1 5 1 6 1 9
1 2 2 5 5 5 6 6 6 7
0 0 0 0 0 0 0 0 0 0
9
1 2 3 4 5 6 7 8 9
1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0
9
1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
0 0 0 0 0 0 0 0 0
3
1 2 3
3 2 1
1 1 1
2 2 2
3 3 3
1 2 3
2 1 2
4 1 2
2 1 2
3 3 1
1 2 2
0 0 0
1
2
3
2
1
0
2
2 2
1 2
1 3
3 1
2 1
2 2
0 0
4
1 2 1 2
2 1 1 2
2 1 2 1
1 2 2 1
1 3 3 1
3 1 3 1
0 0 0 0
0
I get the following answers:

Code: Select all

Game 1:
    (1,1)
    (2,0)
    (1,2)
    (1,2)
    (4,0)
Game 2:
    (2,4)
    (3,2)
    (5,0)
    (7,0)
Game 3:
    (1,0)
Game 4:
    (1,0)
    (1,0)
Game 5:
    (1,2)
    (1,0)
    (1,0)
    (1,0)
    (3,0)
    (0,2)
    (0,2)
    (0,2)
    (0,2)
    (2,0)
Game 6:
    (0,0)
    (1,0)
    (0,0)
Game 7:
    (1,0)
    (0,0)
    (0,0)
    (1,0)
    (2,0)
Game 8:
    (2,2)
    (0,4)
    (2,2)
    (1,1)
    (0,2)
Which is the expected (?) my question is if someone could give me a counterexample?
Sorry for the long post, just felt like it would be fun to find the error...

/Hans
Last edited by dt96hasv on Wed Oct 20, 2004 1:20 pm, edited 1 time in total.

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Wed Oct 20, 2004 11:01 am

Change the type of facit[] and guess[] to int and you'll get accepted.

You read the values of the arrays using scanf("%d",&guess), but since you specify "%d", scanf() will always put 4 bytes (the size of an int) into four consecutive places starting at the address you specified.

dt96hasv
New poster
Posts: 5
Joined: Wed Oct 20, 2004 9:27 am
Location: Gothenburg
Contact:

Post by dt96hasv » Wed Oct 20, 2004 11:13 am

Gosh, that is soo sloppy of me.
I could have spent days looking for that bug, thanx a lot!

Post Reply

Return to “Volume 3 (300-399)”