fork download
  1. program binaryrectangle; (*si legge tutta la matrice M*N e si contano gli '1' e contemporaneamente si tiene conto della posizione degli '1', devono essere tutti dentro un rettangolo di area uguale al numero count di '1' trovati*)
  2. Uses Math;
  3. const
  4. MAXN = 1000;
  5.  
  6. var
  7. M, N, T, ans, i, j, count, infx, infy, supx, supy, test : LongInt;
  8. line : Array[0..MAXN-1] of AnsiString;
  9. c : char;
  10. begin
  11. {
  12.   uncomment the two following lines if you want to read/write from files
  13.   assign(input, 'input.txt'); reset(input);
  14.   assign(output, 'output.txt'); rewrite(output);
  15. }
  16.  
  17. ReadLn(T);
  18. for test:=1 to T do
  19. begin
  20. ReadLn(N, M);
  21.  
  22. for i:=0 to N-1 do
  23. ReadLn(line[i]);
  24. count := 0; infx := N; infy := M; supx := -1; supy := -1;
  25. for i := 0 to N-1 do
  26. begin
  27. for j:= 1 to M do
  28. begin
  29. c:=line[i][j];
  30. if (c = '1') then
  31. begin
  32. inc(count);
  33. infx := min(infx, i);
  34. infy := min(infy, j);
  35. supx := max(supx, i);
  36. supy := max(supy, j);
  37. end;
  38. end;
  39. end;
  40. ans:=0;
  41. if ((count <> 0) and ((supx - infx + 1) * (supy - infy + 1) = count)) then ans:=1
  42. else ans:=0;
  43. WriteLn(ans);
  44. end;
  45. end.
  46.  
Success #stdin #stdout 0s 5324KB
stdin
5
2 2
11
11
2 3
100
110
3 3
100
000
000
4 4
0000
1101
1101
0000
2 2
00
00

stdout
1
0
1
0
0