UESTC_棋盘游戏 CDOJ 578
最近昀昀学习到了一种新的棋盘游戏,这是一个在一个N×N的格子棋盘上去搞M个棋子的游戏,游戏的规则有下列几条:
- 棋盘上有且仅有一个出口
- 开始时没有哪个棋子在出口,而且所有棋子都不相邻(这里的相邻是指上下左右四个方向)
- M个棋子分别记为1到M
- 每个时刻你可以移动一个棋子向它相邻的四个格子移动一步
- 你需要保证任意时刻棋盘上所有棋子都不相邻
- 只有当前棋盘上编号最小的棋子移动到出口时才能取走改棋子。
- 所有棋子都移走的时候游戏结束
对于一个给定的游戏局面,昀昀最少要多少步才能结束这个游戏呢?
Input
第一行有一个整数T,(T≤200),表示测试数据的组数。
对于每一组数据,第一行是两个整数N,M,其中2≤N≤6, 1≤M≤4。
接下来是一个N×N的棋盘,其中o代表空格子,x代表出口,其余的1到M代表这M个棋子。
保证数据是合法的。
Output
对于每一组数据输出最少步数,如果无解输出−1。
Sample input and output
| Sample Input | Sample Output |
|---|---|
2 |
7 |
解题报告
自己也是太年轻。。以为是道大水题,直接上bfs爆搜,果断TLE。。
略思考后显然是A*算法(大水题。。。曼哈顿距离呐)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue> using namespace std; const int Max= ;
int N,M,tid,ttx,tty;
char G[Max][Max];
bool vis[][][][]; typedef struct status{
int pos[],step,pol,f;
friend bool operator < (status a,status b)
{
if (a.step + a.f < b.step + b.f) return false;
if (a.step + a.f > b.step + b.f) return true;
if (a.step < b.step) return false;
else
return true;
}
}; priority_queue<status>q;
status start;
int pol;
int dir[][] = {,,,-,,,-,}; /*A* */
int caculatef(status& x){
int result = ;
for(int i = ;i<M;++i)
result += (abs(x.pos[i] / N - ttx) + abs(x.pos[i] % N - tty) );
return result;
} bool judge(status &x){
for(int i = x.pol;i < M;++i)
for(int j = i;j < M;++j)
if (i != j)
{
int tx1 = x.pos[i] / N;
int ty1 = x.pos[i] % N;
int tx2 = x.pos[j] / N;
int ty2 = x.pos[j] % N;
if (tx1 == tx2 && abs(ty1 - ty2) == ) return false;
else if(ty1 == ty2 && abs(tx1-tx2) == ) return false;
else if(tx1 == tx2 && ty1 == ty2) return false;
}
return true;
} int bfs(){
memset(vis,false,sizeof(vis));
vis[start.pos[]][start.pos[]][start.pos[]][start.pos[]] = true;
start.step = ;
q.push(start);
while(!q.empty())
{
status temp = q.top();q.pop();
if(temp.pos[temp.pol] == tid)
temp.pol++;
if (temp.pol == M) return temp.step;
for(int i = temp.pol;i<M;++i)
for(int j = ;j<;++j)
{
int newx = temp.pos[i] / N + dir[j][];
int newy = temp.pos[i] % N + dir[j][];
if (newx >= N || newx < || newy >= N || newy < ) continue;
int nid = newx * N + newy;
status nst = temp;
nst.pos[i] = nid;
if (!judge(nst)) continue;
if (vis[nst.pos[]][nst.pos[]][nst.pos[]][nst.pos[]] ) continue;
vis[nst.pos[]][nst.pos[]][nst.pos[]][nst.pos[]] = true;
nst.step = temp.step + ;
nst.f = caculatef(nst);
q.push(nst);
}
} return -;
} int main(int argc,char * argv[]){
int T;
scanf("%d",&T);
while(T--)
{
while(!q.empty())
q.pop();
for(int i = ;i<;++i)
start.pos[i] = ;
start.pol = ;
scanf("%d%d%*c",&N,&M);
for(int i = ;i<N;++i)
gets(G[i]);
for(int i = ;i<N;++i)
for(int j = ;j<N;++j)
if(G[i][j] <= '' && G[i][j] >='')
start.pos[G[i][j] - ''] = i*N+j;
else if(G[i][j] == 'x')
tid = i*N + j;
ttx = tid / N;
tty = tid % N;
start.f = caculatef(start);
cout << bfs() << endl;
}
return ;
}
UESTC_棋盘游戏 CDOJ 578的更多相关文章
- UESTC_握手 CDOJ 913
握手 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status ...
- UESTC_冰雪奇缘 CDOJ 843
艾莎女王又开始用冰雪魔法盖宫殿了. 她决定先造一堵墙,于是释放魔法让形为直角梯形的冰砖从天而降,定入冻土之中. 现在你将回答女王的询问:某段冻土上冰砖的面积. 注:多块冰砖之间会互相重叠,重叠部分要多 ...
- UESTC_敢说就敢做 CDOJ 631
敢说就敢做 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...
- UESTC_方老师的分身 II CDOJ 915
方老师的分身 II Time Limit: 10000/5000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_方老师分身 I CDOJ 914
方老师分身 I Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit S ...
- UESTC_神秘绑架案 CDOJ 881
神秘绑架案 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...
- UESTC_方老师买表 CDOJ 885
老师买表 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Stat ...
- UESTC_冬马党 CDOJ 882
冬马党 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Statu ...
- UESTC_魔法少女小蟹 CDOJ 710
小蟹是一名魔法少女,能熟练的施放很多魔法. 有一天魔法学院上课的时候出现了这样一道题,给一个6位数,让大家用自己的魔法,把这个6位数变成另一个给定的6位数. 小蟹翻了下魔法书,发现她有以下6种魔法: ...
随机推荐
- C# WEB API ApiController 修改response header contentType
var res = Request.CreateResponse(HttpStatusCode.OK, file); res.Content.Headers.ContentType = new Med ...
- HDOJ-1016 Prime Ring Problem(DFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数 ...
- OpenStack术语名词及问题调试
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnoAAAEkCAIAAAA3pAtBAAAgAElEQVR4nOy953tUx7o9uCrt2EmtHB
- operator模块
# -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #========== ...
- deciaml(十进制浮点运算)
# -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #========== ...
- easyUI的combobox设置隐藏和显示
今天遇到一个需求,需要在combobox选择不同选项时,分别切换另一个控件为text或者combobox. 当时想了各种办法,想将combobx和text切换隐藏,但是都没得到自己想要的效果.最终还是 ...
- Oracle - 使用序列+触发器实现主键自增长
Oracle中的自增,不如Sql server那般方便. --.创建序列 CREATE SEQUENCE "TABLE_NAME"."SQ_NAME" MINV ...
- Asp.net数据库缓存依赖
Asp.net数据库缓存依赖 更多的时候,我们的服务器性能损耗还是在查询数据库的时候,所以对数据库的缓存还是显得特别重要,上面几种方式都可以实现部分数据缓存功能.但问题是我们的数据有时候是在变化的,这 ...
- VS2015预览版中的C#6.0 新功能(二)
VS2015预览版中的C#6.0 新功能(一) VS2015预览版中的C#6.0 新功能(三) 自动属性的增强 只读自动属性 以前自动属性必须同时提供setter和getter方法,因而只读属性只能通 ...
- OpenCV——肤色检测
一.RGB color space 检测代码如下: void SkinRGB(IplImage* src,IplImage* dst) { //RGB颜色空间 //均匀照明:R>95,G> ...