博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1693 Eat the Trees
阅读量:4637 次
发布时间:2019-06-09

本文共 4450 字,大约阅读时间需要 14 分钟。

Problem Description
Most of us know that in the game called DotA(Defense of the Ancient), Pudge is a strong hero in the first period of the game. When the game goes to end however, Pudge is not a strong hero any more.
So Pudge’s teammates give him a new assignment—Eat the Trees!
The trees are in a rectangle N * M cells in size and each of the cells either has exactly one tree or has nothing at all. And what Pudge needs to do is to eat all trees that are in the cells.
There are several rules Pudge must follow:
I. Pudge must eat the trees by choosing a circuit and he then will eat all trees that are in the chosen circuit.
II. The cell that does not contain a tree is unreachable, e.g. each of the cells that is through the circuit which Pudge chooses must contain a tree and when the circuit is chosen, the trees which are in the cells on the circuit will disappear.
III. Pudge may choose one or more circuits to eat the trees.
Now Pudge has a question, how many ways are there to eat the trees?
At the picture below three samples are given for N = 6 and M = 3(gray square means no trees in the cell, and the bold black line means the chosen circuit(s))
 

 

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains the integer numbers N and M, 1<=N, M<=11. Each of the next N lines contains M numbers (either 0 or 1) separated by a space. Number 0 means a cell which has no trees and number 1 means a cell that has exactly one tree.
 

 

Output
For each case, you should print the desired number of ways in one line. It is guaranteed, that it does not exceed 2
63 – 1. Use the format in the sample.
 

 

Sample Input
2 6 3 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 2 4 1 1 1 1 1 1 1 1
 

 

Sample Output
Case 1: There are 3 ways to eat the trees. Case 2: There are 2 ways to eat the trees.
 
 
裸插头dp
#include
#include
#include
#include
using namespace std;const int bi=1e8;const int MAXN=531441;int i;struct na{ int x,z; na(int xx,int zz):x(xx),z(zz){}};int n,m,x,y,z,a[21],k,p1,p2,en,t;bool map[14][14];long long f[2][MAXN+1],ans;int v[2][MAXN+1];queue
q;inline int gx(int x,int q1,int q2){k=0;for (register int i=m+1;i;i--) k=k*3+(i==x?q1:(i==x+1?q2:a[i]));return k;}inline void up(int x,int z,long long lj){ x++; k=x%2; if (v[k][z]!=x) v[k][z]=x,f[k][z]=0,q.push(na(x,z)); f[k][z]=f[k][z]+lj;}char c[10];int main(){ register int i,j; scanf("%d",&t); for (int u=1;u<=t;u++){ ans=0; memset(map,0,sizeof(map)); memset(v,0,sizeof(v)); scanf("%d%d",&n,&m); for (j=1;j<=n;j++) for (i=1;i<=m;i++) scanf("%d",&map[i][j]); en=n*m-1; while(!map[en%m+1][en/m+1]&&en>0) en--; f[0][0]=v[0][0]=1; q.push(na(0,0)); while(!q.empty()){ na no=q.front();q.pop(); unsigned long long an=f[no.x%2][no.z]; if(no.x%m==0) no.z*=3; x=no.x%m+1;y=no.x/m+1; for (i=1;i<=m+1;i++) a[i]=0; for (i=1,j=no.z;j;i++,j/=3) a[i]=j%3; if (!map[x][y]){ if (no.x==en) ans=ans+an;else up(no.x,gx(x,0,0),an); }else if (a[x]==1&&a[x+1]==2){ if (no.x==en) ans=ans+an; else up(no.x,gx(x,0,0),an);/*多回路*/ }else if (a[x]==2&&a[x+1]==1) up(no.x,gx(x,0,0),an);else if (a[x]==0&&a[x+1]==0){ if (map[x][y+1]&&map[x+1][y]) up(no.x,gx(x,1,2),an); }else if (a[x]==0){ if (map[x+1][y]) up(no.x,gx(x,0,a[x+1]),an); if (map[x][y+1]) up(no.x,gx(x,a[x+1],0),an); }else if (a[x+1]==0){ if (map[x+1][y]) up(no.x,gx(x,0,a[x]),an); if (map[x][y+1]) up(no.x,gx(x,a[x],0),an); }else if (a[x]==a[x+1]){ p1=p2=0; if (a[x]==1) for (j=0,i=x+2;i<=m;i++){ if (a[i]==1) j--; if (a[i]==2) j++; if (j>0&&!p1) p1=i,j--; if (j>0&&p1){p2=i;break;} }else for (j=0,i=x-1;i;i--){ if (a[i]==1) j++; if (a[i]==2) j--; if (j>0&&!p2) p2=i,j--; if (j>0&&p2){p1=i;break;} } a[p1]=1;a[p2]=2;up(no.x,gx(x,0,0),an); } } printf("Case %d: There are %I64d ways to eat the trees.\n",u,ans); }}

 

转载于:https://www.cnblogs.com/Enceladus/p/5137160.html

你可能感兴趣的文章
pip install 问题
查看>>
Vagrant 入门 - 配置
查看>>
Luogu P1315 观光公交
查看>>
vue-router导航守卫,限制页面访问权限
查看>>
UNDERSTANDING CALLBACK FUNCTIONS IN JAVASCRIPT
查看>>
玩法详细说明文档
查看>>
Archlinux GNOME 3 操作习惯的变更
查看>>
visual studio 2005 常用按键
查看>>
2019 Multi-University Training Contest 1 - 1012 - NTT
查看>>
谭浩强C程序设计(第四版)例题中的代码优化
查看>>
浏览器调试淘宝首页看到有趣的招聘信息
查看>>
ASP.NET Identity “角色-权限”管理 4
查看>>
[转][译]ASP.NET MVC 4 移动特性
查看>>
SOC CPU
查看>>
get_result --perl
查看>>
163镜像地址
查看>>
ehcache memcache redis 三大缓存男高音
查看>>
eclipse 快捷键Open Implementation 直接退出
查看>>
minix中管道文件和设备文件的读写
查看>>
JAXB - Annotations, Annotations for Enums: XmlEnum, XmlEnumValue
查看>>