题意:给定两个NxN的棋盘,每个棋盘都有一个‘车’的摆放状态,问进行若干次交换,能否使棋盘1变为棋盘2.

交换规则:每次选两个‘车’,坐标分别为(r1,c1),(r2,c2),如果r1<r2并且c1>c2,则可以将两个点分别移到(r1,c2),(r2,c1)。

做法:BFS,从状态Y1广搜,每次选两行r1,r2(r1<r2),如果满足r1行的‘车’的列c1<c2(r2行的‘车’的列),则交换,得到下一个状态点,如果不是目标态,则继续搜索,知道交换完所有状态。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
using namespace std;
#define N 57 typedef vector<int> VI; int equal(VI ka,VI kb)
{
int len = ka.size();
for(int i=;i<len;i++)
if(ka[i] != kb[i])
return ;
return ;
} class MovingRooksDiv2
{
private:
public:
string move(VI Y1,VI Y2)
{
int len = Y1.size();
string res = "Impossible";
set<VI > vis;
queue<VI > que;
que.push(Y1);
vis.insert(Y1);
while(!que.empty())
{
VI now = que.front();
que.pop();
if(equal(now,Y2)) //到达目标态
{
res = "Possible";
break;
}
for(int r1=;r1<len;r1++)
{
for(int r2=r1+;r2<len;r2++) //r1<r2
{
VI v = now;
if(v[r1] <= v[r2]) //不满足c1>c2
continue;
swap(v[r1],v[r2]); //交换两个'车'
if(!vis.count(v))
{
que.push(v);
vis.insert(v);
}
}
}
}
return res;
}
};

Topcoder SRM 618 Div2 --900的更多相关文章

  1. topcoder SRM 618 DIV2 MovingRooksDiv2

    一开始Y1,Y2两个参数看不懂,再看一遍题目后才知道,vector<int>索引代表是行数,值代表的是列 此题数据量不大,直接深度搜索即可 注意这里深度搜索的访问标识不是以前的索引和元素, ...

  2. topcoder SRM 618 DIV2 WritingWords

    只需要对word遍历一遍即可 int write(string word) { ; ; i < word.length(); ++ i){ cnt+=word[i]-; } return cnt ...

  3. topcoder SRM 618 DIV2 LongWordsDiv2

    此题给出的条件是: (1)word的每个字母都是大写字母(此条件可以忽略,题目给的输入都是大写字母) (2) 相等字符不能连续,即不能出现AABC的连续相同的情况 (3)word中不存在字母组成xyx ...

  4. TopCoder SRM 701 Div2 Problem 900 ThueMorseGame(博弈+预处理)

    题意  Alice和Bob在玩一个游戏,Alice先手. 每次一个人可以从一堆式子中拿走任意数量(不超过m)的式子. 取走最后一颗式子的人胜利. 当一个取完某一步的时候剩下的石子数量的二进制表示中1的 ...

  5. Topcoder Srm 673 Div2 1000 BearPermutations2

    \(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...

  6. Topcoder Srm 671 Div2 1000 BearDestroysDiv2

    \(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...

  7. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  8. Topcoder srm 632 div2

    脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...

  9. topcoder SRM 628 DIV2 BracketExpressions

    先用dfs搜索所有的情况,然后判断每种情况是不是括号匹配 #include <vector> #include <string> #include <list> # ...

随机推荐

  1. css中important的用处

    今天看代码时遇到一段不理解的地方. #note_content { line-height: 22px; border: #DEDEDE 1px solid; background: #FAFAFA; ...

  2. svn 大全

    环境:Win7 32 bit SVN简介:程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是一 ...

  3. Android笔记——Windows环境下Android Studio v1.0安装教程

    本文主要讲解Windows环境下Android Studio的安装教程,Mac的Android Studio安装与此类似不在赘述,另外友情提示Windows下的SDK与Mac的SDK是通用的,可以直接 ...

  4. [Xamarin.iOS] Visual Studio中Xamarin.iOS项目,无法加入PCL项目参考、NuGet组件参考

    [Xamarin.iOS] Visual Studio中Xamarin.iOS项目,无法加入PCL项目参考.NuGet组件参考 解决方案 目前Visual Studio中最新版本的Xamarin.iO ...

  5. JS中检测数据类型的四种方法

    1.typeof 用来检测数据类型的运算符->typeof value->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."st ...

  6. SharePoint 2013 的HTML5特性之响应式布局

    今天偶然看到一本书<Pro SharePoint 2013 Branding and Responsive Web Development>,看到SharePoint 2013基于HTML ...

  7. C#获取本地系统日期格式

    我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...

  8. Enforcing the correct protocol for partially SSL secured SharePoint sites

    Enforcing the correct protocol for partially SSL secured SharePoint sites http://www.sharepointconfi ...

  9. Android项目实战(十):自定义倒计时的TextView

    项目总结 -------------------------------------------------------------------------------------------- 有这 ...

  10. [Android]IllegalStateException: Could not find method onBind(View)

    FATAL EXCEPTION: main Process: org.diql.aidldemo, PID: 2274 java.lang.IllegalStateException: Could n ...