现在的位置: 首页 > 综合 > 正文

蓝桥杯 剪格子

2018年04月08日 ⁄ 综合 ⁄ 共 1226字 ⁄ 字号 评论关闭
就是从0,0开始走一个凸多边形出来,找出走出一半的最小步数…… 

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int ans,half,n,m;
int pic[20][20];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
bool vis[20][20];
bool isbeyond(int x,int y)
{
	return x<0||y<0||x>=n||y>=m;
}
void dfs(int x,int y,int step,int sum)
{
	int i,tx,ty;
	if(step>=ans||sum>half)
		return;
	if(sum==half)
	{
		ans=step;
		return;
	}
	if(isbeyond(x,y))
		return;
	for(i=0;i<4;i++)
	{
		tx=x+dx[i];
		ty=y+dy[i];
		if(vis[tx][ty])
			continue;
		vis[tx][ty]=1;
		dfs(tx,ty,step+1,sum+pic[x][y]);
		vis[tx][ty]=0;
	}
}
int main()
{
	int i,j;
	cin>>m>>n;
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			cin>>pic[i][j];
			half+=pic[i][j];
		}
	if(half&1)
	{
		cout<<0;
		return 0;
	}
	half/=2;
	ans=110;
	dfs(0,0,0,0);
	if(ans==110)
		cout<<0;
	else
		cout<<ans;
	return 0;
}

历届试题 剪格子  

时间限制: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

抱歉!评论已关闭.