fork download
  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.io.PrintWriter;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. new Resolver(System.in, System.out).solve();
  10. }
  11.  
  12. public static class Resolver {
  13.  
  14. private Scanner scanner;
  15. private PrintWriter writer;
  16.  
  17. this.scanner = new Scanner(is);
  18. this.writer = new PrintWriter(os);
  19. }
  20.  
  21. public void solve() {
  22. int rowCount = scanner.nextInt();
  23. int columnCount = scanner.nextInt();
  24. int[][] chocolate = new int[rowCount][columnCount];
  25. for (int i = 0; i < rowCount; i++) {
  26. for (int j = 0; j < columnCount; j++) chocolate[i][j] = scanner.nextInt();
  27. }
  28. check(chocolate, rowCount, columnCount);
  29. writer.flush(); writer.close();
  30. }
  31.  
  32. private void check(int[][] chocolate, int rowCount, int columnCount) {
  33. if (mayBreakByRow(chocolate, rowCount, columnCount) || mayBreakByColumn(chocolate, rowCount, columnCount)) writer.println("Yes");
  34. else writer.println("No");
  35. }
  36. private boolean mayBreakByRow(int[][] chocolate, int rowCount, int columnCount) {
  37. for (int i = 0; i < rowCount - 1; i++) {
  38. boolean okRow = true;
  39. for (int j = 0; j < columnCount; j++) {
  40. if (chocolate[i][j] == chocolate[i + 1][j]) { okRow = false; break; }
  41. }
  42. if (okRow) return true;
  43. }
  44. return false;
  45. }
  46. private boolean mayBreakByColumn(int[][] chocolate, int rowCount, int columnCount) {
  47. for (int i = 0; i < columnCount - 1; i++) {
  48. boolean okColumn = true;
  49. for (int j = 0; j < rowCount; j++) {
  50. if (chocolate[j][i] == chocolate[j][i + 1]) { okColumn = false; break; }
  51. }
  52. if (okColumn) return true;
  53. }
  54. return false;
  55. }
  56. }
  57. }
Success #stdin #stdout 0.06s 2184192KB
stdin
2 3
1 1 2
3 3 2
stdout
Yes