HDU 4478 Where is King
题目大意:
一个王可以向周围8个方格走,如果都不通留在原地,t秒后,他可能存在的位置数
这题数据量过大,我们需要通过奇偶性判断,如果t = 0可以到达,说明 t=2,4,6.。。。都可以到达
所以我这用dp[N][N][2] 来记录x,y位置上奇数和偶数时间分别到达那点的最短时间,如果不存在,用-1表示
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = ;
int dp[N][N][],n,t,x,y,vis[N][N];
char mat[N][N];
int dir[][] = {{,},{-,},{,},{,-},{,-},{,},{-,},{-,-}}; struct Node{
int x,y;
Node(int x,int y):x(x),y(y){}
}; queue<Node> q; void bfs()
{
q.push(Node(x,y));
vis[x][y] = ;
while(!q.empty()){
Node t = q.front();
q.pop();
vis[t.x][t.y]=; for(int i=;i<;i++){
int xx = t.x + dir[i][];
int yy = t.y + dir[i][];
if(xx>=&&xx<=n&&yy>=&&yy<=n&&mat[xx][yy] == '.'){
int flag = ;
if((dp[xx][yy][] < || dp[xx][yy][] > dp[t.x][t.y][] + ) && dp[t.x][t.y][] >= )
{
dp[xx][yy][] = dp[t.x][t.y][] + ;
flag = ;
}
if((dp[xx][yy][] < || dp[xx][yy][] > dp[t.x][t.y][] + ) && dp[t.x][t.y][] >= )
{
dp[xx][yy][] = dp[t.x][t.y][] + ;
flag = ;
} if(flag && !vis[xx][yy])
{
vis[xx][yy]=;
q.push(Node(xx,yy));
}
}
}
}
}
int main()
{
//freopen("test.in","rb",stdin);
//cout << "Hello world!" << endl;
int C;
scanf("%d",&C);
while(C--){
scanf("%d%d%d%d",&n,&t,&x,&y);
for(int i=;i<=n;i++)
scanf("%s",mat[i]+); memset(vis,,sizeof(vis));
memset(dp,-,sizeof(dp));
dp[x][y][]=; bfs(); int tmp = t&;
int ans = ;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(dp[i][j][tmp]!=- && dp[i][j][tmp]<=t)
ans++;
}
}
printf("%d\n",max(ans,));
}
return ;
}
HDU 4478 Where is King的更多相关文章
- hdu 5201 The Monkey King【容斥原理+组合数学】
原来我一开始以为的\( O(n^2) \)是调和级数\( O(nlog_2n) \)的! 首先枚举猴王的桃子个数\( x \),然后使用容斥原理,枚举有至少\( k \)个不满足的条件,那么这\( k ...
- HDU - 1512 Monkey King
Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each ...
- HDU 3861.The King’s Problem 强联通分量+最小路径覆盖
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 4489 The King's Ups and Downs
HDU 4489 The King's Ups and Downs 思路: 状态:dp[i]表示i个数的方案数. 转移方程:dp[n]=∑dp[j-1]/2*dp[n-j]/2*C(n-1,j-1). ...
- HDU 4489 The King’s Ups and Downs dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4489 The King's Ups and Downs Time Limit: 2000/1000 ...
- HDU 5642 King's Order dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order Accepts: 381 Submissions: 1361 ...
- HDU 5644 King's Pilots 费用流
King's Pilots 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 Description The military parade w ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- HDU 5642 King's Order 动态规划
King's Order 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 Description After the king's speec ...
随机推荐
- 二进制流BinaryFormatter存储读取数据的细节测试
二进制流的使用很方便,为了更好的理解应用它,我创建简单对象开始测试它的增加特性和减少特性. [Serializable] class Data----------开始时候的存储对象 { public ...
- 在 c#中 如何 重新激活一个控件
比如toolBar是一个组合控件 this.toolBar.CaptionHeight =this.toolBar.Items.Count * 60;//重新激活toolBar控件 CaptionHe ...
- sed附加命令
追加命令(命令a) sed '[address] a the-line-to-append' input-file 在第二行后面追加一行(原文这里可能有问题,没有写名行号) [root@sishen ...
- Spring Mvc相关随笔
web.xml部分 1.欢迎界面 <welcome-file-list> <welcome-file>/views/login.jsp</welcome-file> ...
- R in action 读书笔记(1)--第五章:高级数据管理
5.2.1数学函数 函数 描述 abs(x) 绝对值 sqrt(x) 平方根 ceiling(x) 不小于x的最小整数 floor(x) 不大于x的最大整数 trunc(x) 向0的方向截取的X中的整 ...
- Farseer.net轻量级开源框架 中级篇:常用的扩展方法
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: BasePage.BaseController.BaseHandler.BaseM ...
- WebSocket 的一些简单页面推送使用
因为做通信项目的时候,需要实时获取每个分机的当前状态,发现websocket还不错,只是对浏览器的要求比较高, 针对特定用户推送消息,网上有一些 public class GetHttpSession ...
- xcode 通配搜索
class \w*<\w*> extension \w*: \w* \{\} 搜索所有泛型类.
- swift 与 NSObject
以NSObject为基类,只是为了提供Objective-C API的使用入口: 经由@object修改的对象,是这些api的参量. NSObject是swift与oc特有机制沟通的桥梁. Subcl ...
- python爬虫学习:分布式抓取
前面的文章都是基于在单机操作,正常情况下,一台机器无论配置多么高,线程开得再多,也总会有一个上限,或者说成本过于巨大.因此,本文将提及分布式的爬虫,让爬虫的效率提高得更快. 构建分布式爬虫首先需要有多 ...