您的位置:首页 > 其它

历届试题 剪格子

2017-02-24 19:47 190 查看
历届试题 剪格子

时间限制:1.0s 内存限制:256.0MB

问题描述

如下图所示,3 x 3 的格子中填写了一些整数。

+--*--+--+
|10* 1|52|
+--****--+
|20|30* 1|
*******--+
| 1| 2| 3|
+--+--+--+
我们沿着图中的星号线剪开,得到两个部分,每个部分的数字和都是60。

本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以分割为两个部分,使得这两个区域的数字和相等。

如果存在多种解答,请输出包含左上角格子的那个区域包含的格子的最小数目。

如果无法分割,则输出 0。

输入格式

程序先读入两个整数 m n 用空格分割 (m,n<10)。

表示表格的宽度和高度。

接下来是n行,每行m个正整数,用空格分开。每个整数不大于10000。

输出格式
输出一个整数,表示在所有解中,包含左上角的分割区可能包含的最小的格子数目。
样例输入1
3 3
10 1 52
20 30 1
1 2 3
样例输出1
3
样例输入2
4 3
1 1 1 1
1 30 80 2
1 1 1 100
样例输出2
10

真坑啊,忽略了是先输入列数再输入行数,另外后台测试数据太水了。

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#define FOR(i,x,n) for(long i=x;i<n;i++)
#define ll long long int
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAX_N 50005

using namespace std;

struct node{
int x,y;
int sum;
int pathCounter;
int jud[20][20];
node(){
FOR(k,0,20){
FOR(l,0,20){
jud[k][l]=0;
}
}
}
};

int N,M;
int a[20][20];
int summ=0;
int half=0;
int ans=0;
int dirY[4]={0,0,-1,1};
int dirX[4]={1,-1,0,0};
queue<node> q;

int dfs(){
while(!q.empty()){
node t=q.front();q.pop();
FOR(i,0,4){
node next=t;
next.x+=dirX[i];next.y+=dirY[i];
next.pathCounter++;
next.sum+=a[next.x][next.y];
if(next.sum==half&&next.jud[next.x][next.y]==0){
ans=next.pathCounter;
return 1;
}
if(next.jud[next.x][next.y]==0&&next.x>=0&&next.x<N&&next.y>=0&&next.y<M){
next.jud[next.x][next.y]=1;
q.push(next);
}
}
}
return 0;
}

int main()
{
//freopen("data.txt", "r", stdin);
//freopen("data.out", "w", stdout);
node n;
n.x=0;n.y=0;
n.pathCounter=1;
n.sum=0;
n.jud[0][0]=1;
scanf("%d %d",&M,&N);
FOR(i,0,N){
FOR(j,0,M){
scanf("%d",&a[i][j]);
summ+=a[i][j];
}
}
n.sum+=a[0][0];
q.push(n);
half=summ/2;
int aaa=dfs();
if(aaa==0){
printf("0\n");
}else{
printf("%d\n",ans);
}

//fclose(stdin);
//fclose(stdout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: