POJ 3221 Diamond Puzzle.
~~~~
id=3221
显然是BFS找最优解。但是终止条件不好写。看到有一仅仅队交上去一直TLE。
比赛完了看题解原来是以目标状态为起点,BFS给每一个状态打表。用一个map映射存起来。
~~~~
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<map>
using namespace std; char str[10];
char s[10]="0123456";
map<string,int> v; //状态映射
map<string,int>ans; //答案映射
struct node
{
int t;
char s[10];
};
//分别在0~6位置能够到达的目标位置,以-1结束。 int dir[7][4]={
{2,4,6,-1},{2,6,-1},{1,0,3,-1},{2,4,-1},
{3,0,5,-1},{4,6,-1},{1,0,5,-1} };
int Find(char* s)
{
for(int i=0;i<7;i++)
if(s[i]=='0') return i;
}
int bfs()
{
queue<node> q;
node cur,next;
strcpy(cur.s,s);cur.t=0;
q.push(cur);
v[cur.s]=1;
ans[cur.s]=0;
while(!q.empty())
{
cur=q.front(); q.pop();
int pos=Find(cur.s); //'0'的位置。 for(int i=0;dir[pos][i]!=-1;i++)
{
char temp[10];
strcpy(temp,cur.s);
//交换‘0’和和对应能去的位置
swap(temp[pos],temp[dir[pos][i]]);
if(!v[temp])
{
v[temp]=1;
strcpy(next.s,temp);
next.t=cur.t+1;
ans[next.s]=next.t;
q.push(next);
}
}
}
}
int main()
{
v.clear();
ans.clear();
bfs();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
if(strcmp(str,s)==0) puts("0");
else printf("%d\n",ans[str]==0?-1:ans[str]);
}
return 0;
}
POJ 3221 Diamond Puzzle.的更多相关文章
- POJ 3221 Diamond Puzzle(BFS)
Description A diamond puzzle is played on a tessellated hexagon like the one shown in Figure 1 below ...
- poj 1651 Multiplication Puzzle (区间dp)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
- poj 3678 Katu Puzzle(2-sat)
Description Katu Puzzle ≤ c ≤ ). One Katu ≤ Xi ≤ ) such that for each edge e(a, b) labeled by op and ...
- POJ 3678 Katu Puzzle (经典2-Sat)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 2401 Descr ...
- POJ 1651 Multiplication Puzzle(类似矩阵连乘 区间dp)
传送门:http://poj.org/problem?id=1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K T ...
- POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- poj 1651 Multiplication Puzzle
题目链接:http://poj.org/problem?id=1651 思路:除了头尾两个数不能取之外,要求把所有的数取完,每取一个数都要花费这个数与相邻两个数乘积的代价,需要这个代价是最小的 用dp ...
- poj 3678 Katu Puzzle(Two Sat)
题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
随机推荐
- spring aop 内部调用问题解决
方法1: 基于 proxy 的 spring aop 带来的内部调用问题可以使用 AopContext.currentProxy() 强转为当前的再调用就可以解决了 例如: 错误用法:public A ...
- Java之三大基础排序(冒泡、选择、插入)
注:以下排序均为从小到大 一.冒泡排序 package com.yunche.testsort; import java.util.Arrays; /** * @ClassName: BubbleSo ...
- idea安装及使用
使用:https://blog.csdn.net/qq_42303709/article/details/81983208 安装教程:https://blog.csdn.net/yl171272518 ...
- 折线分割平面(hdoj 2050,动态规划递推)
Problem Description 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平面 ...
- ROS lesson 1
ROS ROS官网 ROS 简介 ROS 是 Robot Operation System 的简写,并且 他诞生在2000年后,至今有10余年了,运行在 Linux(Ubuntu) 上 ROS 不是 ...
- UVA - 12113 Overlapping Squares(dfs+回溯)
题目: 给定一个4*4的棋盘和棋盘上所呈现出来的纸张边缘,问用不超过6张2*2的纸能否摆出这样的形状. 思路: dfs纸的张数,每一张中枚举这张纸左上角这个点的位置,暴力解题就可以了. 这个题的覆盖太 ...
- Vijos P1334 NASA的食物计划
解法 二维费用01背包问题 解法就是先枚举物品再枚举条件这里两个条件所以要枚举两个for 代码 #include <bits/stdc++.h> using namespace std; ...
- FileWriter实现从一个文件中读取内容并写到另一个文件中
FileWriter和FileOutputStream都是向文件写内容,区别是前台一次写一个字符,后者一次写一个字节 package com.janson.day20180827; import ja ...
- sublime 使用笔记
unbuntu安装sublime---------------------------------------------sudo add-apt-repository ppa:webupd8team ...
- Python面向对象之面向对象封装案例
面向对象封装案例 封装 封装是面型对象编程的一大特点 面向对象编程的第一步--将属性和方法封装到一个抽象的类中: 外界使用类创建对象,然后让对象调用方法: 对象方法的细节都被封装在类的内部. 一个对象 ...