链接:http://acm.hust.edu.cn/vjudge/problem/11229

题意:给定一个n*m的矩阵,矩阵中有两个2和两个3,其他的为0/1,0表示空地,1表示障碍。要求将2连到2,3连到3并且两条线不能相交,求最短距离。

分析:大白书轮廓线dp例题,用3进制保存状态,枚举所有转移时的状态。注意新的一行开始时要将上一行最后那个点的状态的轮廓线右移一位。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=12;
const int mod=1000000007;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1000000000;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
int t[N],s[N],ma[N][N],dp[2][60000],f[60000][10];
void deal(int n) {int i,j;t[0]=1;s[0]=2;for (i=1;i<N;i++) t[i]=t[i-1]*3,s[i]=t[i]*2;for (i=0;i<n;i++)for (j=0;j<10;j++) f[i][j]=(i/t[j])%3;
}
int main()
{int i,j,k,n,m,mx,now,pre,L,U,news,newt,last;deal(60000);while (scanf("%d%d", &n, &m)&&(n||m)) {for (i=0;i<n;i++)for (j=0;j<m;j++) scanf("%d", &ma[i][j]);now=pre=dp[0][0]=0;mx=t[m+1]-1;for (i=1;i<=mx;i++) dp[0][i]=100;for (i=0;i<n;i++)for (j=0;j<m;j++) {pre=now;now^=1;for (k=0;k<=mx;k++) dp[now][k]=100;for (k=0;k<=mx;k++)if (dp[pre][k]!=100) {if (j==0&&k*3>=t[m+1]) continue ;j==0 ? last=k*3:last=k;L=f[last][j];U=f[last][j+1];if (ma[i][j]==0) {newt=news=last;if (L==0&&U==0) {newt+=t[j]+t[j+1];news+=s[j]+s[j+1];dp[now][last]=min(dp[now][last],dp[pre][k]);dp[now][newt]=min(dp[now][newt],dp[pre][k]+1);dp[now][news]=min(dp[now][news],dp[pre][k]+1);} else if (L==0&&U==1) {newt+=t[j]-t[j+1];dp[now][last]=min(dp[now][last],dp[pre][k]+1);dp[now][newt]=min(dp[now][newt],dp[pre][k]+1);} else if (L==0&&U==2) {news+=s[j]-s[j+1];dp[now][last]=min(dp[now][last],dp[pre][k]+1);dp[now][news]=min(dp[now][news],dp[pre][k]+1);} else if (L==1&&U==0) {newt+=t[j+1]-t[j];dp[now][last]=min(dp[now][last],dp[pre][k]+1);dp[now][newt]=min(dp[now][newt],dp[pre][k]+1);} else if (L==1&&U==1) {newt-=t[j]+t[j+1];dp[now][newt]=min(dp[now][newt],dp[pre][k]+1);} else if (L==2&&U==0) {news+=s[j+1]-s[j];dp[now][last]=min(dp[now][last],dp[pre][k]+1);dp[now][news]=min(dp[now][news],dp[pre][k]+1);} else if (L==2&&U==2) {news-=s[j]+s[j+1];dp[now][news]=min(dp[now][news],dp[pre][k]+1);}} else if (ma[i][j]==1) {if (L==0&&U==0) dp[now][last]=min(dp[now][last],dp[pre][k]);} else if (ma[i][j]==2) {if (L==0&&U==0) {dp[now][last+t[j]]=min(dp[now][last+t[j]],dp[pre][k]);dp[now][last+t[j+1]]=min(dp[now][last+t[j+1]],dp[pre][k]);}if (L==1&&U==0) {dp[now][last-t[j]]=min(dp[now][last-t[j]],dp[pre][k]);}if (L==0&&U==1) {dp[now][last-t[j+1]]=min(dp[now][last-t[j+1]],dp[pre][k]);}} else {if (L==0&&U==0) {dp[now][last+s[j]]=min(dp[now][last+s[j]],dp[pre][k]);dp[now][last+s[j+1]]=min(dp[now][last+s[j+1]],dp[pre][k]);}if (L==2&&U==0) {dp[now][last-s[j]]=min(dp[now][last-s[j]],dp[pre][k]);}if (L==0&&U==2) {dp[now][last-s[j+1]]=min(dp[now][last-s[j+1]],dp[pre][k]);}}}}if (dp[now][0]!=100) printf("%d\n", dp[now][0]+2);else printf("0\n");}return 0;
}

uvalive3620Manhattan Wiring相关推荐

  1. POJ 3133 Manhattan Wiring (插头DP)

    Manhattan Wiring Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1110   Accepted: 634 D ...

  2. arduino编程语言Wiring参考手册API

    对Arduino的编程是利用 Arduino编程语言 (基于 Wiring)和Arduino开发环境(based on Processing)来实现的. 通过编程,Arduino可以实现很多种功能. ...

  3. 探索Wiring Pi

    wIring pi ,专门为树莓派打造的类似于arduino下的wiring驱动库 安装好这个库后可以直接调用函数配置和控制GPIO功能 (无论是用c shell  python都能控制GPIO了) ...

  4. Wiring in Spring: @Autowired, @Resource and @Inject 三种注解实现依赖注入

    原文链接:Wiring in Spring: @Autowired, @Resource and @Inject 1. Overview 概述 In this Spring Framework tut ...

  5. wiring的运行(使用open source产品组装你的web应用架构)

    前一阵在csdn上看到totodo翻译的使用open source产品组装你的web应用架构,感觉不错,于是乎将wiring.zip下载下来,结果运行的比较不顺利,现将运行过程出现的磕磕绊绊以及解决办 ...

  6. Spring基本概念:IoC,DI,Wiring

    文章目录 一.IoC(控制反转) 二.DI(依赖注入) 三.Bean configuration(配置) 1. XML Schema-based configuration:基于XML模式的配置 2. ...

  7. ServiceMix异常(1):missing requirement [262.0] osgi.wiring.package;osgi.wiring.package=org.apache.camel

    环境 servicemix :6.1.0 jdk:jdk1.8.0_191 项目中使用的camel版本:2.23.1 servicemix中安装的camel版本:2.16.1 起初小编在开发的时候就直 ...

  8. poj3133 Manhattan Wiring

    Manhattan Wiring Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2016   Accepted: 1162 ...

  9. POJ 3133 Manhattan Wiring(限制匹配的插头DP)

    Manhattan Wiring Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1921   Accepted: 1130 ...

最新文章

  1. decorator java_装饰器模式-Decorator(Java实现)
  2. Python:向函数传递任意数量的实参
  3. OpenCASCADE:Direct3D CSharp 示例
  4. Linux命令之su -- 用于切换当前用户身份到其他用户身份
  5. 前端学习(3050):vue+element今日头条管理-表格组件基本使用
  6. Mysql学习总结(61)——MySQL优化之DBA级优化整理汇总
  7. 无需充电的太阳能汽车有多强?约17万起,续航高达1609km
  8. [leetcode]90. 子集 II
  9. 网络管理软件免费linux,最新Xmanager Power Suite6网络管理工具免费官方下载6.0.199 - 系统之家...
  10. JavaSE学习--面向对象
  11. 接口测试 Jmeter面试题
  12. 雷霆战机单机老版本_雷霆战机单机版
  13. 【开发教程6】疯壳·ARM功能手机-中断实验教程
  14. python爬取12306_详解python 爬取12306验证码
  15. 数据可视化Error:matplotlib is required for plotting when the default backend “matplotlib“ is selected
  16. Ubuntu 20.04安装velodyne_simulator
  17. 到底是加瓦好还是C好呢?
  18. ios13如何隐藏第三方应用(苹果ios13怎么隐藏个别软件)
  19. MySQL 数据类型
  20. html字体设置大小打印无效,css font-size属性怎么不管用?

热门文章

  1. RSI指标顶背离趋势反转信号,文华财经期货RSI底背离自动画线指标公式源码
  2. 深澜系统服务器架构,智圣新创|深澜4K 3a Radius进程故障处理案例
  3. Spring源码深度解析(郝佳)-学习-jdk代理-cglib代理
  4. it系统应急响应流程图_智能自动化的内网应用系统应急响应处置的方法与流程...
  5. 易保全上线“工信部查询”新功能,用户可在工信部直接查询存证数据
  6. 云linux电脑,在Linux下可用Wine安装和运行达龙云电脑、极云普惠云电脑
  7. 【精】领扣LintCode算法问题答案:336. 文本压缩
  8. 达内oracle教材,达内学习心得oracle数据库笔记
  9. cad lisp 示坡线_示坡线画法详解
  10. 老板急坏了,公司网站的 HTTPS 过期了