POJ 1233

有些代码往往需要编码者一定能坐的了板凳,这道题在大佬眼里可能不算啥,但是自己这方面能力真的有待提升,压得住性子写下去

题目本身思路很简单,只不过写起来变量名繁多,中间多次错误,数据也看不出来,最后还是借助对拍别人的AC代码才敢确定。

这是一个需要锻炼的能力以及理想的写代码的境界,即,一个复杂的代码写出来你能够对代码运行结果和质量就是能够有那个自信。这份自信不是从别出来,正是对自身能力的自信。

#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
using namespace std;

const int maxn= 15;
const int maxt= 1005;

struct Node
{
	int t, x, y;
	Node(int _t= 0, int _x= 0, int _y= 0) : t(_t), x(_x), y(_y) {}
};
int n, m;
char ct[maxt][maxn][maxn];
int vis[maxt][maxn][maxn];
int step[2][6][2]= {{{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {0, -1}}, 
					{{-1, -1}, {-1, 0}, {0, 1}, {1, 0}, {1, -1}, {0, -1}}};

int BFS()
{
	if (1== n){
		for (int i= 1; i<= m; ++i){
			if ('C'== ct[0][1][i]){
				return 0;
			}
		}
		return -1;
	}
	queue<Node> Q;
	memset(vis, 0, sizeof(vis));
	for (int i= 1; i<= m; ++i){
		if ('C'== ct[0][1][i]){
			Q.push(Node(0, 1, i));
		}
	}
	int t, x, y, nt, s_idx;
	Node cur;

	while (!Q.empty()){
		cur= Q.front();
		Q.pop();
		t= cur.t;
		x= cur.x;
		y= cur.y;
		nt= t+1;
		s_idx= x & 1;
		if (nt>= 1000){
			break;
		}
		if ('C'== ct[nt][x][y] && !vis[nt][x][y]){
			Q.push(Node(nt, x, y));
			vis[nt][x][y]= 1;
		}

		for (int i= 0; i< 6; ++i){
			int nx= x+step[s_idx][i][0], ny= y+step[s_idx][i][1];
			if ('C'== ct[nt][nx][ny] && !vis[nt][nx][ny]){
				if (n== nx){
					return nt;
				}
				Q.push(Node(nt, nx, ny));
				vis[nt][nx][ny]= 1;
			}
		}
	}

	return -1;
}

int main(int argc, char const *argv[])
{
	int kase, ans;
	scanf("%d", &kase);

	for (int i= 0; i<= 1001; ++i){
		memset(ct[i][0], 0, sizeof(ct[i][0]));
	}
	while (kase--){
		scanf("%d %d", &n, &m);
		for (int i= 1; i<= n; ++i){
			scanf(" %s", ct[0][i]+1);
			ct[0][i][0]= 0;
		}
		memset(ct[0][n+1], 0, sizeof(ct[0][n+1]));
		for (int i= 1; i<= 1001; ++i){
			for (int j= 1; j<= n; ++j){
				int st_idx= j & 1;
				for (int k= 1; k<= m; ++k){
					int c_cnt= 0;
					for (int p= 0; p< 6; ++p){
						int x= j+step[st_idx][p][0], y= k+step[st_idx][p][1];
						if ('C'== ct[i-1][x][y]){
							++c_cnt;
						}
					}
					if ('H'== ct[i-1][j][k]){
						ct[i][j][k]= 3== c_cnt ? 'C' : 'H';
					}
					else{
						ct[i][j][k]= 3== c_cnt || 2== c_cnt ? 'C' : 'H';
					}
				}
				ct[i][j][m+1]= 0;
				ct[i][j][0]= 0;
			}
			memset(ct[i][n+1], 0, sizeof(ct[i][n+1]));
		}

		ans= BFS();
		if (-1== ans){
			printf("impossible
");
		}
		else{
			printf("%d
", ans+1);
		}
	}	

	return 0;
}
原文地址:https://www.cnblogs.com/Idi0t-N3/p/14675041.html