Page 1 of 2

340 - Master-Mind Hints

Posted: Sun Aug 18, 2002 3:47 pm
by arc16
the problem is simple, but i got WA
is there any tricky case?

Posted: Sat Aug 31, 2002 3:46 am
by arc16
my stupid mistake :P
somehow i set 100 for the max size where it should be 1000 :D

Posted: Mon Dec 23, 2002 7:47 pm
by deddy one
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??

Posted: Sun Dec 29, 2002 2:27 pm
by epsilon0
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

Posted: Mon Dec 30, 2002 8:01 pm
by deddy one
my output is


(1,0)



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

Posted: Tue Dec 31, 2002 2:49 am
by deddy one
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

340--help!!

Posted: Mon May 26, 2003 6:52 am
by ericpo
I keep get WA,so is there anyone can give me some test data??
PLEASE....><

Posted: Wed Jun 04, 2003 11:10 pm
by chfgress
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

340 why WA?

Posted: Mon Jul 07, 2003 6:13 am
by chunyi81
[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.

Re: 340 why WA?

Posted: Mon Jul 07, 2003 6:36 am
by Observer
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!!

Re: 340 why WA?

Posted: Mon Jul 07, 2003 10:32 am
by chunyi81
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.

340 why WA

Posted: Mon Jul 07, 2003 11:53 am
by chunyi81
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.

340 - Master-Mind hints

Posted: Wed Oct 20, 2004 9:45 am
by dt96hasv
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

Posted: Wed Oct 20, 2004 11:01 am
by little joey
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.

Posted: Wed Oct 20, 2004 11:13 am
by dt96hasv
Gosh, that is soo sloppy of me.
I could have spent days looking for that bug, thanx a lot!