6993: Dominoes(纯bfs)
题目描述
Orz likes to play dominoes. Now giving an n*m chessboard and k dominoes whose size are 1*2, Orz finds that there is exactly one grid empty, so that he can move dominoes without overlapping them. An initial situation is given, he wants to know how many final situation could be achieved, except the initial situation. Note every domino is different, as they have their own serial number. Since the answer may be very large, please output the answer modulo 1000000009.
输入
There will be multiple test cases. For each test case:
The first line contains three integers: n, m, k(n ≤ 9, m ≤ 10000).
The following k lines, each line contains four integers: a b c d, indicating that this domino occupies (a, b) and (c, d).
The input guarantees that the domino does not overlap, and there is exactly one empty grid.
输出
For each test cases, output the answer modulo 1000000009.
样例输入
5 5 12
1 1 2 1
1 2 2 2
1 3 2 3
1 4 1 5
2 4 2 5
3 4 3 5
3 1 3 2
4 1 4 2
5 1 5 2
4 3 5 3
4 4 5 4
4 5 5 5
样例输出
8
题意:类似华容道,问空格能在多少个位置出现
多米诺牌是1*2大小的,它可以横着放,也可以竖着放。
题目给出了k个多米诺牌的两个格子的坐标。
牌横着放的时候两个格子的横坐标相等,记为1;竖着放的时候两个格子的纵坐标相等,记为2。
空格可以上下左右四个方向移动:
如果空格能向上向下移动,那么,空格上下的牌必须是竖着放的;
如果空格能向左向右移动,那么,空格左右的牌必须是横着放的。
首先判断空格上下左右的牌是如何放置的,再判断空格能否移动。
标记空格走过的位置,空格出现的位置的状态是唯一的。
纯bfs,一层一层的搜。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
int MAP[][],ans,vis[][],n,m;
int dir[][]= {-,,,,,-,,};
struct node
{
int x,y;
};
queue<node>p;
int judge(int x,int y)
{
if(x>=&&x<=n&&y>=&&y<=m)
return ;
else
return ;
}
void bfs_(int tx,int ty)
{
node q;
q.x=tx,q.y=ty;
p.push(q);//将空格的位置入队列
vis[tx][ty]=;
int sx,sy;
while(!p.empty())
{
node temp;
temp=p.front();
p.pop();
vis[temp.x][temp.y]=;
node L;
sx = temp.x;
sy = temp.y;
if(MAP[sx-][sy]==&&judge(sx-,sy)&&vis[sx-][sy]==)//空格上面的牌是竖着放的,牌只能向下移动
{ //(sx-2,sy)是移动之后空格的坐标
ans++;
L.x=sx-,L.y=sy;
vis[sx-][sy]=;
p.push(L);
}
if(MAP[sx+][sy]==&&judge(sx+,sy)&&vis[sx+][sy]==)//空格下面的牌是竖着放的,牌只能向上移动
{
ans++;
L.x=sx+,L.y=sy;
vis[sx+][sy]=;
p.push(L);
}
if(MAP[sx][sy-]==&&judge(sx,sy-)&&vis[sx][sy-]==)//空格左边的牌是横着放的,牌只能向右移动
{
ans++;
L.x=sx,L.y=sy-;
vis[sx][sy-]=;
p.push(L);
}
if(MAP[sx][sy+]==&&judge(sx,sy+)&&vis[sx][sy+]==)//空格右边的牌是横着放的,牌只能向左移动
{
ans++;
L.x=sx,L.y=sy+;
vis[sx][sy+]=;
p.push(L);
}
} }
int main()
{
int k,tx,ty;
while(~scanf("%d%d%d",&n,&m,&k))
{
int x1,y1,x2,y2;
ans=;
memset(MAP,,sizeof(MAP));
memset(vis,,sizeof(vis));
for(int i=; i<=k; i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1 == x2)
MAP[x1][y1]=,MAP[x2][y2]=;
else
MAP[x1][y1]=,MAP[x2][y2]=;
}
int flag = ;
for(int i=; i<=n; i++) //找到空格的位置
{
for(int j=; j<=m; j++)
{
if(MAP[i][j] == )
{
tx=i,ty=j;
flag = ;
break;
}
}
if(flag)
break;
}
bfs_(tx,ty);
printf("%d\n",ans);
}
return ;
}
6993: Dominoes(纯bfs)的更多相关文章
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- codevs 3290 华容道(SPFA+bfs)
codevs 3290华容道 3290 华容道 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 小 B 最近迷上了华容道,可是 ...
- poj3669 Meteor Shower(预处理+bfs)
https://vjudge.net/problem/POJ-3669 先给地图a[][]预处理每个位置被砸的最小时间.然后再bfs. 纯bfs,还被cin卡了下时间.. #include<io ...
- 小结:bfs
概要: 我们在初始状态要到达终止状态可以沿着同深度的向下搜索,这样范围覆盖更广,在解的深度较小的时候十分适用. 技巧及注意: 所有状态在转移后如果要打标记一定要在进队列前打!不要在出队列才打!否则就是 ...
- poj2312Battle City BFS
题意: M行N列矩阵, 'Y'表示开始位置, 'T'表示目标位置, 从开始位置到目标位置至少需要走多少步,其中, 'S', 'R'表示不能走, 'B' 花费为2, 'E'花费为1. 思路:纯 BFS. ...
- POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)
题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- 【wikioi】1026 逃跑的拉尔夫
题目链接 算法:BFS 14.01.02 PS: 本人再次脑残,BFS又是写得那么脓肿,突然发现我原来什么搜索都是不会的呀.. //2014-02-05已更新 ******************** ...
- B - Dungeon Master POJ - 2251
//纯bfs #include <iostream> #include <algorithm> #include <cstring> #include <cs ...
随机推荐
- C++ 基于凸包的Delaunay三角网生成算法
Delaunay三角网,写了用半天,调试BUG用了2天……醉了. 基本思路比较简单,但效率并不是很快. 1. 先生成一个凸包: 2. 只考虑凸包上的点,将凸包环切,生成一个三角网,暂时不考虑Delau ...
- sosreport-汇总收集linux系统信息
有时候我们想将Linux服务器信息一次性查询并导出发送给他人,我们可以使用这个sosreport这个工具 安装 yum install sos -y 收集信息 sosreport 收集到的信息默认保 ...
- WinForm控件开发总结目录
WinForm控件开发总结(一)------开篇 WinForm控件开发总结(二)------使用和调试自定义控件 WinForm控件开发总结(三)------认识WinForm控件常用的Attrib ...
- mysql入门学习笔记
MySQL的登陆和退出 mysql -u 用户名 -p 密码 #登陆 quit #退出(exit or \q) 具体参数: 参数 描述 -D,--database=name 打开指定数据库 -deli ...
- h5需要的浏览器插件
google浏览器插件: web前段助手.vue-tools.草料二维码
- Windows7环境下Excel2010中图片超链接默认打开程序修改
在Windows7的环境下,打开Excel2010超链接图片,都是默认用IE打开.搜索了所有的中文网络,没有找到解决方案,且发现受此困扰的人不少.后来用纯英文搜索,终于找到解决方案并尝试成功. 1,操 ...
- C++类的组合例子
Line类调用Point类的两个对象p1,p2作为其数据成员,计算线段长度 组合类构造函数定义的一般形式为: 类名::类名(形参表):内嵌对象1(形参表),内嵌对象2(形参表)... {类的初始化} ...
- uWSGI的配置与发布
参考: https://www.cnblogs.com/pyyu/p/9481344.html 一.什么是wsgi, uwsgi, uWSGI wsgi 全称web server gateway ...
- 将项目打成jar包执行 在liunx上执行 java -xx.jar
一:普通maven java项目 项目目录 pom.xml <?xml version="1.0" encoding="UTF-8"?> <p ...
- WebApi的好处和MVC的区别
1.WebApiwebapi有自己的路由. webservice和wcf的协议都是soap协议,数据的序列化和反序列化都是soap的格式.而webapi是Json的数据传递 webapi的优点有哪些? ...