HDU 4619 Warm up 2

Warm up 2

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)


Problem Description
  Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizontally or vertically. It's guaranteed the dominoes in the same direction are not overlapped, but horizontal and vertical dominoes may overlap with each other. You task is to remove some dominoes, so that the remaining dominoes do not overlap with each other. Now, tell me the maximum number of dominoes left on the board.
 

Input
  There are multiple input cases.
  The first line of each case are 2 integers: n(1 <= n <= 1000), m(1 <= m <= 1000), indicating the number of horizontal and vertical dominoes.
Then n lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x + 1, y).
  Then m lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x, y + 1).
  Input ends with n = 0 and m = 0.
 

Output
  For each test case, output the maximum number of remaining dominoes in a line.
 

Sample Input
  
2 3 0 0 0 3 0 1 1 1 1 3 4 5 0 1 0 2 3 1 2 2 0 0 1 0 2 0 4 1 3 2 0 0
 

Sample Output
  
4 6
 

Source
2013 Multi-University Training Contest 2

训练的时候想出了一个算法:找出各个由横竖牌交错连接起来的整体面积,最后的牌数等于各个(面积+1)/2相加。当时没从理论上证明。
后来别人总理论上证明了:把每个牌当做一个点,牌有重叠部分代表点之间有连线。那么这些连起来的点形成一条线,且相邻的点不能同时取,那么能取的点数(即牌数)是这个(线的点数+1)/2;

开始写的代码超时,是因为代码没处理好细节,导致无限循环,通过设置count记录循环的次数,证明了这一点。

#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

struct Node{
	int x,y;
}a[1100],b[1100];

bool vis[2][1100];

vector<int> son[2][1100];

int main()
{
	int n,m;
	int i,j;

	while(scanf("%d%d",&n,&m),n||m)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&a[i].x,&a[i].y);
			son[0][i].clear();
		}
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&b[i].x,&b[i].y);
			son[1][i].clear();
		}

		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				if(a[i].x==b[j].x && a[i].y==b[j].y || a[i].x+1==b[j].x && a[i].y==b[j].y
					|| a[i].x==b[j].x && a[i].y==b[j].y+1 || a[i].x+1==b[j].x&&a[i].y==b[j].y+1)
				{
					son[0][i].push_back(j);	son[1][j].push_back(i);
				}

			}
		}

		int sum=0,tmp,flag,d;
		int count;
		memset(vis,0,sizeof(vis));
		for(i=1;i<=n;i++)
		{
			if(!vis[0][i])
			{
				if(son[0][i].size()==0)
				{
					sum++;
					continue;
				}
				vis[0][i]=true;
				tmp=son[0][i][0];	flag=1;	d=2;
				vis[1][tmp]=1;
				count=1;
				while(son[flag][tmp].size()!=1)
				{
					if(!vis[(flag+1)%2][son[flag][tmp][0]])
						tmp=son[flag][tmp][0];
					else if(!vis[(flag+1)%2][son[flag][tmp][1]])
						tmp=son[flag][tmp][1];
					else
						break;
					flag=(flag+1)%2;
					vis[flag][tmp]=true;
					d++;
					count++;
					if(count>3000)
					{
						printf("Error\n");
						break;
					}
				}
				if(son[0][i].size()==2 && !vis[1][son[0][i][1]])
				{
					tmp=son[0][i][1];	flag=1;	d++;
					vis[1][tmp]=1;
					count=1;
					while(son[flag][tmp].size()!=1)
					{
						if(!vis[(flag+1)%2][son[flag][tmp][0]])
						tmp=son[flag][tmp][0];
						else if(!vis[(flag+1)%2][son[flag][tmp][1]])
							tmp=son[flag][tmp][1];
						else
							break;
						flag=(flag+1)%2;
						vis[flag][tmp]=true;
						d++;
						count++;
						if(count>3000)
						{
							printf("Error\n");
							break;
						}
					}
				}
				sum+=(d+1)/2;
			}
		}
		for(i=1;i<=m;i++)
		{
			if(!vis[1][i])
				sum++;
		}
		printf("%d\n",sum);
	}

	return 0;
}


提供了基于BP(Back Propagation)神经网络结合PID(比例-积分-微分)控制策略的Simulink仿真模型。该模型旨在实现对杨艺所著论文《基于S函数的BP神经网络PID控制器及Simulink仿真》中的理论进行实践验证。在Matlab 2016b环境下开发,经过测试,确保能够正常运行,适合学习和研究神经网络在控制系统中的应用。 特点 集成BP神经网络:模型中集成了BP神经网络用于提升PID控制器的性能,使之能更好地适应复杂控制环境。 PID控制优化:利用神经网络的自学习能力,对传统的PID控制算法进行了智能调整,提高控制精度和稳定性。 S函数应用:展示了如何在Simulink中通过S函数嵌入MATLAB代码,实现BP神经网络的定制化逻辑。 兼容性说明:虽然开发于Matlab 2016b,但理论上兼容后续版本,可能会需要调整少量配置以适配不同版本的Matlab。 使用指南 环境要求:确保你的电脑上安装有Matlab 2016b或更高版本。 模型加载: 下载本仓库到本地。 在Matlab中打开.slx文件。 运行仿真: 调整模型参数前,请先熟悉各模块功能和输入输出设置。 运行整个模型,观察控制效果。 参数调整: 用户可以自由调节神经网络的层数、节点数以及PID控制器的参数,探索不同的控制性能。 学习和修改: 通过阅读模型中的注释和查阅相关文献,加深对BP神经网络与PID控制结合的理解。 如需修改S函数内的MATLAB代码,建议有一定的MATLAB编程基础。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值