zoukankan      html  css  js  c++  java
  • 棋盘游戏

    题目描述

    思路

    bfs主要问题就是走重复路的问题,需要设置一个vis数组防止走重复路,怎么把一个二维数组表示成一个数,扩展来说就是怎么把状态表示成一个数,看了别人的代码,可以二进制的数来记录状态,4x4的数组状态不会超过2^(17)。
    然后就是怎么走的问题,规定只能向下,下右走,而向上,向左就是向下,向右的逆运算。if筛选能够向下,向右走的点就可以了。

    代码

    #include <cstdio>
    #include <cstring> 
    #include <queue>
    
    char st[5][5], ed[5][5];
    int js[5][5], num_ed, tmp, cur, v, vis[135000];
    std::queue<int> q;
    void init() {
    	for (int i = 0; i < 4; ++i) {
    		for (int j = 0; j < 4; ++j) {
    			js[i][j] = 1 << (i * 4 + j);
    		}
    	}
    }
    int get(char arr[][5]) {
    	int res = 0;
    	for (int i = 0; i < 4; ++i) {
    		for (int j = 0; j < 4; ++j) {
    			res += js[i][j] * (arr[i][j] - '0');
    		}
    	}
    	return res;
    }
    void trans(int x, char arr[][5]) {
    	for (int i = 3; i >= 0; --i) {
    		for (int j = 3; j >= 0; --j) {
    			arr[i][j] = x / js[i][j] + '0';
    			x %= js[i][j];
    		}
    	}
    }
    void swap(char &x, char &y) {
    	char t = x;
    	x = y;
    	y = t;
    }
    int main() {
    	for (int i = 0; i < 4; ++i) scanf("%s", st[i]);
    	for (int i = 0; i < 4; ++i) scanf("%s", ed[i]);
    	init();
    	num_ed = get(ed);
    	tmp = get(st);
    	q.push(tmp);
    	memset(vis, 0x3f, sizeof(vis));
    	vis[tmp] = 0;
    	while (!q.empty()) {
    		cur = q.front();
    		v = vis[cur];
    		q.pop();
    		if (cur == num_ed) break;
    		trans(cur, st);
    		for (int i = 0; i < 4; ++i) {
    			for (int j = 0; j < 4; ++j) {
    				if (i < 3) {
    					swap(st[i][j], st[i + 1][j]);
    					tmp = get(st);
    					if (vis[tmp] > v + 1) {
    						q.push(tmp);
    						vis[tmp] = v + 1;
    					}
    					swap(st[i +  1][j], st[i][j]);
    				}
    				if (j < 3) {
    					swap(st[i][j], st[i][j + 1]);
    					tmp = get(st);
    					if (vis[tmp] > v + 1) {
    						q.push(tmp);
    						vis[tmp] = v + 1;
    					}
    					swap(st[i][j], st[i][j + 1]);
    				}
    			}
    		}
    	}
    	printf("%d", vis[num_ed]);
    	return 0;
    }
    
  • 相关阅读:
    NOJ 1116 哈罗哈的大披萨 【淡蓝】 [状压dp+各种优化]
    Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]
    noj 2069 赵信的往事 [yy题 无限gcd]
    noj 2068 爱魔法的露露 [线性扫一遍]
    Codeforces Round #275 (Div. 2) B. Friends and Presents 二分+数学
    Word2007文档中怎么输入上标下标
    Linux下查找命令
    矩阵求逆
    LRC算法在人脸识别中应用
    java从txt文档读写数据
  • 原文地址:https://www.cnblogs.com/liuzz-20180701/p/11602456.html
Copyright © 2011-2022 走看看