Nine degrees OJ topic 1250: Matrix transformation

Source: Internet
Author: User

Title Description:

For an integer matrix, there is an operation to add a moment to any element in the matrix, which needs to be adjacent (up or down) and one element,
A positive matrix is given to determine whether it can be obtained by the above operation from a full 0 matrix.

Input:

Output:

If the output "Yes" can be transformed, otherwise "No".
There are multiple sets of data, the first row of each group of data a positive integer n (n<=10), a n*n matrix, followed by n rows, n integers per row. When n is 0 o'clock, the test ends.

Sample input:
31 10 91 1 21 0 130 1 00 1 21 0 10
Sample output:
YesNo

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <algorithm>5#include <iostream>6#include <cmath>7 intmatrix[ A][ A];8 intdir[][2] = {{0,1},{0,-1},{1,0},{-1,0}};9 Ten intMainintargcChar Const*argv[]) One { A     intN; -      while(SCANF ("%d", &n)! = EOF && N! =0)  { -         intx =0, y =0; the          for(inti =0; I < n; i++) { -              for(intj =0; J < N; J + +) { -scanf"%d",&matrix[i][j]); -                 if((i + j) &1) { +x = x +Matrix[i][j]; -                 } +                 Else { Ay = y +Matrix[i][j]; at                 } -             } -         } -         if(X! =y) { -Puts"No"); -             Continue; in         } -         BOOLIsOk =true; to          for(inti =0; I < n && isOk; i++) { +              for(intj =0; J < n && IsOk; J + +) { -                 intsum =0; the                  for(intp =0; P <4; p++) { *                     intTMPX = i + dir[p][0]; $                     intTmpy = j + dir[p][1];Panax Notoginseng                     if(Tmpx >=0&& tmpx < n && tmpy >=0&& Tmpy <N) { -sum = sum +Matrix[tmpx][tmpy]; the                     } +                 } A                 if(Matrix[i][j] >sum) { theIsOk =false; +                      Break; -                 } $             } $         } -         if(isOk) { -Puts"Yes"); the         } -         Else {WuyiPuts"No"); the         } -  Wu  -     } About     return 0; $}

The key to this problem is to find the necessary and sufficient conditions for judgment, there are two necessary and sufficient conditions here (excerpt from http://www.cnblogs.com/liangrx06/p/5083814.html),

(1) x=sum (A[i][j] where i+j is odd, y=sum (A[i][j]) where i+j is even, then there is x=y
(2) Any one element is not more than the surrounding four elements and

Nine degrees OJ topic 1250: Matrix transformation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.