ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1124. Mosaic

Where my program fail?
Posted by Andrey Popyk (popyk@ief.tup.km.ua) 14 Jan 2002 14:50
I use simple simulation of "hand moves".
I cannot create a test, where my program fail, but I have received
WA :-(
Could anyone tell me, where is my bug?

VAR A:Array[1..500,1..50] of integer;
    M,N:integer;

PROCEDURE ReadData;
var P,i,j:integer;
begin
  readln(M,N);
  for i:=1 to M do
  begin
    for j:=1 to N do read(A[i][j]);
    readln;
  end;
end;

FUNCTION MakeChanges(K,P:integer):longint;
var Sum:longint;
    C,i:integer;
    bool:boolean;
begin
  Sum:=0;
  repeat
    inc(Sum);
    C:=A[K][P];
    A[K][P]:=K;
    bool:=true;
    for i:=1 to N do
      if A[C][i]<>C then begin bool:=false; K:=C; P:=i; break end;
  until bool;
  MakeChanges:=Sum;
end;

PROCEDURE Process;
var i,j:integer;
    Sum:longint;
begin
  Sum:=0;
  for i:=1 to M do
    for j:=1 to N do
      if A[i][j]<>i then Sum:=Sum+MakeChanges(i,j);
  writeln(Sum);
end;

BEGIN
  ReadData;
  Process;
END.
You have problems if the graph is not connected - a test for you (+)
Posted by shitty.Mishka 14 Jan 2002 16:34
For example, for this test:
4 1
2
1
4
3

The answer is 5, not 4.
Thanks!
Posted by Andrey Popyk (popyk@ief.tup.km.ua) 14 Jan 2002 17:43
Thanks, I ge AC, but I write new version of solution.
Simulations doesn't work!