题目链接:http://codeforces.com/problemset/problem/711/B

题目大意:

  输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 使得矩阵 行列斜 和全部相等。

代码状态:

一把辛酸泪

2016.09.24 8:14 再次更新  DOWN 【昨天晚上的‘杯具’要谨记,做题要看懂题,看清题的要求再做,切记、切记、切记】

补解题思路:

1.

 1.1 先特殊判断 n 是否==1,如果等于 1 则直接输出 1 即可

 1.2 如果 n > 1 判断 0 的位置在哪个位置,找一个要填入到0位置的数。

    如果 0 在第一行,那么用第二行的和 减去 第一行的和,便是要填入的值.

    如果 0 在最后一行,那么用倒数第二行的和 减去 最后一行的和,便是要填入的值.

    其他的 则 0 在中间位置 ,那么可以用 前一行或者后一行的和 减去 0 所在的和,便是要填入的值.

    (用列减也可以)

    【看清题意,如果填入的值 <1 则输出 -1】

2.

  将要填入的值填到 0 的位置 进行全部的扫描判断即可。全盘的扫描不在解释。

AC Code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=500+5;
 4 typedef  long long ll;
 5 ll na[N][N];
 6 int check(int n)
 7 {
 8     ll ans=0,ans1=0;
 9     int i,j;
10     for(i=1; i<=n; i++)
11         ans+=na[1][i],ans1+=na[i][1];
12     if(ans!=ans1)
13         return 0;
14     ans1=0;
15     ll ans2=0;
16     for(i=1; i<=n; i++)
17     {
18         for(j=1; j<=n; j++)
19             ans1+=na[i][j], ans2+=na[j][i];
20         if(ans!=ans2||ans1!=ans2||ans!=ans1)
21             return 0;
22         ans1=ans2=0;
23
24     }
25     ans1=ans2=0;
26     for(i=1; i<=n; i++)
27     {
28         for(j=1; j<=n; j++)
29             if(i==j)
30                 ans1+=na[i][j],ans2+=na[i][n-i+1];
31     }
32     if(ans!=ans2||ans1!=ans2||ans!=ans1)
33         return 0;
34     return 1;
35 }
36 int main()
37 {
38     int n;
39     while(~scanf("%d",&n))
40     {
41         int i,j;
42         if(n==1)
43         {
44             scanf("%d",&n);
45             printf("1\n");
46             continue;
47         }
48         int x,y;
49         for(i=1; i<=n; i++)
50             for(j=1; j<=n; j++)
51             {
52                 scanf("%d",&na[i][j]);
53                 if(na[i][j]==0)x=i,y=j;
54             }
55         ll ans=0,ans1=0;
56         if(x==1)
57         {
58             for(i=1; i<=n; i++)
59                 ans+=na[1][i],ans1+=na[2][i];
60             na[x][y]=ans1-ans;
61         }
62         else if(x==n)
63         {
64             for(i=1; i<=n; i++)
65                 ans+=na[n][i],ans1+=na[n-1][i];
66             na[x][y]=ans1-ans;
67         }
68         else
69         {
70             for(i=1; i<=n; i++)
71                 ans+=na[x][i],ans1+=na[x-1][i];
72             na[x][y]=abs(ans1-ans);
73         }
74
75         if(check(n))
76             printf("%I64d\n",na[x][y]<1?-1:na[x][y]);
77         else printf("-1\n");
78     }
79     return 0;
80 }

转载于:https://www.cnblogs.com/A--Q/p/5901984.html

codeforces 711B - Chris and Magic Square(矩阵0位置填数)相关推荐

  1. CodeForces - 711B Chris and Magic Square

    CodeForces - 711B  Chris and Magic Square   题意:给你一个N*N矩阵,其中0代表未知的那个数,让你在这个位置填上一个数使整个矩阵的 每一行.每一列.主对角线 ...

  2. Codeforces Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】

    B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  3. B. Chris and Magic Square

    B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. cf369 B Chris and Magic Square

    B. Chris and Magic Square time limit per test2 seconds memory limit per test256 megabytes inputstand ...

  5. Chris and Magic Square

    Chris and Magic Square ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There ...

  6. Chris and Magic Square CodeForces - 711B

    ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...

  7. 文章标题 Chris and Magic Square

    ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...

  8. 【codeforces 711B】Chris and Magic Square

    [题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...

  9. 【CodeForces - 349B】Color the Fence (贪心,填数)

    题干: Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on th ...

最新文章

  1. css图片和文字一样高,css里图片和文字如何等高
  2. 解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
  3. SkyWalking配上告警更优秀
  4. 当学术论文开始标题党「x is All You Need」
  5. iqoo玩游戏怎么回主界面_科普向:iQOO游戏功能到底怎么用?
  6. Xamarin 技术全解析
  7. Android6.0指纹识别开发
  8. python爬取豆瓣电影top250_python3爬取豆瓣top250电影
  9. tenacity库 重试代码
  10. 如何生成java dump文件
  11. 【解决】阿拉伯语等右向左排版文字CSS解决方案
  12. 最菜的我之打卡第三天
  13. 微服务框架springcloud(实用篇)【5】Elasticsearch 01
  14. python os.path.abspath()与os.path.realpath()区别
  15. 服务器的1U ,2U分别是什么意思?
  16. luci与页面的数据交互
  17. Android ContentProvider之联系人数据库及操作
  18. cant download je-7.4.5.jar with maven [ com.sleepycat:je:jar:7.4.5 ]
  19. Java编写的记账本
  20. 独角兽项目 5 - 两个披萨团队和技术债务

热门文章

  1. Struts2学习总结二
  2. spring security认证的底层实现
  3. 豆瓣图书的推荐与搜索、简易版知识引擎构建(neo4j)
  4. Android 7.0 Keyguard流程分析
  5. Mips KVM TrapEmulate implemented in Linux
  6. spring 事务原理_Spring声明式事务处理的实现原理,来自面试官的穷追拷问
  7. mysql handlers,2 Handlers
  8. python爬虫scrapy安装_零基础写python爬虫之爬虫框架Scrapy安装配置
  9. python列表内数字排序_如何在Python中手动排序数字列表?
  10. 图片不能置于底层怎么办_PPT中常遇到的图片问题和解决方案