zoukankan      html  css  js  c++  java
  • 【wikioi】2495 水叮当的舞步(IDA*)

     http://wikioi.com/problem/2495/

    这题我还是看题解啊囧。(搜索实在太弱。完全没想到A*,还有看题的时候想错了,。,- -)

    好吧,估价还是那么的简单,判断颜色不同的数目即可(左上角的联通块不算在内)

    然后A*还是一样的做法。

    迭代加深还是一样的味道~

    在这里我们用c[i][j]来表示左上角开始的联通块和联通块外面一层(因为要从外面一层拓展颜色),分别记为1和2

    那么我们在搜索的时候,染色只要染c[i][j]为2的颜色种类,并且更新联通块(在这里不需要传图,因为一层一层的拓展下去的话,是单调递增的,所以用不到之前的颜色)我们在搜索前只需要维护之前的c数组然后更新完后再还原回来即可。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    #define read(a) a=getnum()
    #define print(a) printf("%d", a)
    inline int getnum() { int ret=0; char c; int k=1; for(c=getchar(); c<'0' || c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0' && c<='9'; c=getchar()) ret=ret*10+c-'0'; return k*ret; }
    
    const int N=8;
    int m[N][N], c[N][N], n, ans;
    int fx[4]={-1, 1, 0, 0}, fy[4]={0, 0, -1, 1};
    bool used[6];
    
    inline int H() {
    	int ret=0;
    	CC(used, 0);
    	rep(i, n) rep(j, n)
    		if(!used[m[i][j]] && c[i][j]!=1)
    			used[m[i][j]]=true, ++ret;
    	return ret;
    }
    
    void change2(const int &x, const int &y, const int &color) {
    	c[x][y]=1;
    	int nx, ny;
    	rep(i, 4) {
    		nx=x+fx[i], ny=y+fy[i];
    		if(nx<0 || nx>=n || ny<0 || ny>=n || c[nx][ny]==1) continue;
    		c[nx][ny]=2;
    		if(m[nx][ny]==color) change2(nx, ny, color);
    	}
    }
    
    inline bool change(const int &color) {
    	bool ret=false;
    	rep(i, n) rep(j, n) if(c[i][j]==2 && m[i][j]==color) ret=true, change2(i, j, color);
    	return ret;
    }
    
    bool dfs(const int &g) {
    	int h=H();
    	if(g+h>ans) return false;
    	if(!h) return true;
    	int t[N][N];
    	memcpy(t, c, sizeof(c));
    	for1(i, 0, 5) {
    		if(change(i) && dfs(g+1)) return true;
    		memcpy(c, t, sizeof(t));
    	}
    	return false;
    }
    
    int main() {
    	for(read(n); n; read(n)) {
    		CC(c, 0); CC(m, 0);
    		rep(i, n) rep(j, n) read(m[i][j]);
    		change2(0, 0, m[0][0]);
    		for(ans=0; ; ++ans)
    			if(dfs(0)) break;
    		printf("%d
    ", ans);
    	}
    	
    	return 0;
    }
    

    题目描述 Description

      水叮当得到了一块五颜六色的格子形地毯作为生日礼物,更加特别的是,地毯上格子的颜色还能随着踩踏而改变。
      为了讨好她的偶像虹猫,水叮当决定在地毯上跳一支轻盈的舞来卖萌~~~

      地毯上的格子有N行N列,每个格子用一个0~5之间的数字代表它的颜色。
      水叮当可以随意选择一个0~5之间的颜色,然后轻轻地跳动一步,左上角的格子所在的联通块里的所有格子就会变成她选择的那种颜色。这里连通定义为:两个格子有公共边,并且颜色相同。
      由于水叮当是施展轻功来跳舞的,为了不消耗过多的真气,她想知道最少要多少步才能把所有格子的颜色变成一样的。

    输入描述 Input Description

      每个测试点包含多组数据。
      每组数据的第一行是一个整数N,表示地摊上的格子有N行N列。
      接下来一个N*N的矩阵,矩阵中的每个数都在0~5之间,描述了每个格子的颜色。
      N=0代表输入的结束。

    输出描述 Output Description

      对于每组数据,输出一个整数,表示最少步数。

    样例输入 Sample Input

    2
    0 0
    0 0
    3
    0 1 2
    1 1 2
    2 2 1
    0

    样例输出 Sample Output

    0
    3

    数据范围及提示 Data Size & Hint

      对于30%的数据,N<=5
      对于50%的数据,N<=6
      对于70%的数据,N<=7
      对于100%的数据,N<=8,每个测试点不多于20组数据。

    第二组样例解释:
      0 1 2       1 1 2       2 2 2      1 1 1
      1 1 2 --> 1 1 2 --> 2 2 2 --> 1 1 1
      2 2 1       2 2 1       2 2 1      1 1 1

    来源:Nescafe 21

  • 相关阅读:
    一句话解释数字签名。一句话解释数字证书
    KVC、KVO实现过程
    SSH基本概念和用途
    UICollectionView左对齐流水布局、右对齐流水布局
    Git命令学习笔记
    Xcode8插件安装
    Xcode日常使用
    dispatch_group_t 日常使用注意事项
    二分查找变种
    maven插件调试方法
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3895999.html
Copyright © 2011-2022 走看看