HDU 4539 郑厂长系列故事――排兵布阵(曼哈顿距离)
这虽然是中文题,然而没看懂,不懂的地方,就是在曼哈顿距离这块,网上搜索了一下,写了个程序,是测试曼哈顿距离的。
曼哈顿距离:两点(x1,y1)(x2,y2)的曼哈顿距离为|x1-x2|+|y1-y2|
测试代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int INF=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++) char tu[60][60];
int a[100][2];
int main()
{
#ifndef ONLINE_JUDGE
// freopen("C:\\Users\\Zmy\\Desktop\\in.txt","r",stdin);
// freopen("C:\\Users\\Zmy\\Desktop\\out.txt","w",stdout);
#endif // ONLINE_JUDGE int i=2,i1=3;
int p=0;
rep(j,6)
rep(j1,6)
{
if (abs(i-j)+abs(i1-j1)==2)
{
printf("%d %d\n",j,j1);
a[p][0]=j;
a[p++][1]=j1;
}
} // i=2,i1=2;
// rep(j,6)
// rep(j1,6)
// {
// if (abs(i-j)+abs(i1-j1)==2)
// {
// printf("%d %d\n",j,j1);
// a[p][0]=j;
// a[p++][1]=j1;
// }
// } cle(tu,'.');
rep(i,p)
{ tu[a[i][0]][a[i][1]]='*';
} rep(i,6)
{
rep(j,6)
{
printf("%c ",tu[i][j]);
}
prN;
} return 0;
}
这是根据测试样例的背景写的,i,i1是所给坐标,最后输出*的地方,就是他对应的曼哈顿距离 (由图可知:曼哈顿距离就是以这个点为中心的菱形)
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 110
#define M 200 using namespace std; int n,m,a[N];
int dp[N][M][M],cnt,num[M],sum[M];
///dp[i][j][k] 表示第i行的j状态 i-1行的k状态 的最优解 bool ok(int x)
{
return (x&(x<<2))||(x&(x<<2));///第x-2个和x+2个位是否有人,
// 有的话就不符合条件
} int getsum(int x) ///1的个数,即放多少个兵
{
int sum=0;
while(x)
{
if(x&1)sum++;
x>>=1;
}
return sum;
} void init()
{
cnt=0;
for(int i=0; i<(1<<m); i++) ///预处理,压缩
{
if(!ok(i))
{
num[cnt]=i;
sum[cnt++]=getsum(i);
}
}
}
////二进制函数
//int two[100];
//void o_two(int n)
//{
// memset(two,0,sizeof(two));
// int p=1;
// while(n)
// {
// two[p++]=n%2;
// n/=2;
// }
// printf("Two:");
// for (int i=12;i>=1;i--)
// {
// if (i%4==0)
// printf(" ");
// printf("%d ",two[i]);
// }puts("");
//}
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\Zmy\\Desktop\\in.txt","r",stdin);
// freopen("C:\\Users\\Zmy\\Desktop\\out.txt","w",stdout);
#endif // ONLINE_JUDGE
while(~scanf("%d%d",&n,&m))
{
memset(a,0,sizeof a);
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
int x;
scanf("%d",&x);
if(x)continue;///方便计算,把0存为1
a[i]|=(1<<j);
}
}
// for (int i=0;i<n;i++)
// o_two(a[i]);
init();///初始化了num(情况数组)和sum(个数数组)
memset(dp,0,sizeof dp);
for(int i=0; i<cnt; i++) ///处理第一行
{
if(a[0]&num[i])continue;
dp[0][i][0]=sum[i];
} ///递推,要考虑三行的情况,
for(int i=1; i<n; i++) //第一行
{
for(int j=0; j<cnt; j++)
{
if(a[i]&num[j])continue; ///第一行与原来矩阵有冲突
for(int k=0; k<cnt; k++) ///第二行
{
if(a[i-1]&num[k])continue;
if(((num[k]<<1)&num[j])||((num[k]>>1)&num[j]))continue; //i-1行
int Max=-1;
for(int l=0; l<cnt; l++) ///第三行
{
if(num[j]&num[l])continue;
if(((num[l]<<1)&num[k])||((num[l]>>1)&num[k]))continue;
Max=max(Max,dp[i-1][k][l]);
}
dp[i][j][k]=Max+sum[j];
}
}
}
int Max=-1;
for(int i=0; i<cnt; i++)
for(int j=0; j<cnt; j++)
Max=max(Max,dp[n-1][i][j]);
printf("%d\n",Max);
}
return 0;
}
HDU 4539 郑厂长系列故事――排兵布阵(曼哈顿距离)的更多相关文章
- HDU 4539郑厂长系列故事――排兵布阵(状压DP)
HDU 4539 郑厂长系列故事――排兵布阵 基础的状压DP,首先记录先每一行可取的所哟状态(一行里互不冲突的大概160个状态), 直接套了一个4重循环居然没超时我就呵呵了 //#pragma co ...
- HDU 4539 郑厂长系列故事——排兵布阵
http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Others) ...
- HDU 4539 郑厂长系列故事——排兵布阵 状压dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事--排兵布阵 Time Limit: 10000/5000 MS (Java/O ...
- HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Ot ...
- POJ 1185 - 炮兵阵地 & HDU 4539 - 郑厂长系列故事——排兵布阵 - [状压DP]
印象中这道题好像我曾经肝过,但是没肝出来,现在肝出来了也挺开心的 题目链接:http://poj.org/problem?id=1185 Time Limit: 2000MS Memory Limit ...
- 郑厂长系列故事——排兵布阵 hdu4539(状态压缩DP)
郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- HDU-4539郑厂长系列故事——排兵布阵(状态压缩,动态规划)
郑厂长系列故事--排兵布阵 Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total ...
- hdu4539 郑厂长系列故事——排兵布阵 + POJ1158 炮兵阵地
题意: 郑厂长系列故事--排兵布阵 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/32 ...
- [状压dp]HDOJ4539 郑厂长系列故事——排兵布阵
中文题,题意不再赘述 对于“?”这一格,它所能攻击到的(曼哈顿距离为2的) 前方的 即“√”的四个位置 那么与此格有关的即它前方两行(即状压这两行) 首先预处理每行能满足的: i 和(i<< ...
随机推荐
- [USACO11JAN]利润Profits
题目描述 The cows have opened a new business, and Farmer John wants to see how well they are doing. The ...
- ZOJ-3870 Team Formation
题目大意:给n个正数,找出满足A^B>max(A,B)的对数. 题目分析: 代码如下: # include<iostream> # include<cstdio> # i ...
- ThinkPHP使用SQL函数进行查询
//SQL函数查询 $products=$pro->where(array("FIND_IN_SET('".$type."',type)",'num'=& ...
- QT GUI @创建新的工程
开发环境: Qt 4.5 Qt Creator 1.3.0 新工程创建步骤: 1. 单击运行Qt Creator,进入欢迎页面.选择"File" -> "New F ...
- Java Memory Model
http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html http://www.ibm.com/developerworks/libr ...
- PHP-网页跳转的几种方式
本文总结了跳转到指定网页的几种方式. 1.利用PHP的header函数Location响应头, header是用来向浏览器返回HTTP响应头(详细请看HTTP协议详解) <?php header ...
- VS 2012 C#快捷键
ctrl + J 重现智能提示 ctrl + L 删除一行ctrl + K ctrl + C 注释选中行ctrl +K ctrl +U 取消注释 ctrl +K ctrl +F 格式 ...
- luke 操作记录
精确查询(不需要切分词):得使用KeywordAnalyzer而不是StandardAnalyzer,原因如下: StandardAnalyzer:
- c# webBrowser控件与js的交互
转自:http://blog.csdn.net/sd2131512/article/details/7467564 [System.Runtime.InteropServices.ComVisible ...
- Python_进程、线程及协程
一.Python进程 IO密集型----多线程 计算密集型----多进程 1.单进程 from multiprocessing import Process def foo(i): print('你好 ...