Submission #118311


Source Code Expand

#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <list>
#include <cassert>
#include <unordered_map>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define EPS 1e-9
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;

#if defined(_MSC_VER)
int popcnt(unsigned long long x) {
	int r = 0;
	while(x) r ++, x = x & (x-1);
	return r;
}
#else
inline int popcnt(unsigned long long x) { return __builtin_popcountll(x); }
#endif

int L, H, W, empties;
string a[8];
typedef unsigned long long ull;
pii dxdys[4] = {mp(0,1),mp(1,0),mp(0,-1),mp(-1,0)};
map<pair<ull,unsigned>,bool> memo;
unordered_map<ull,bool> badmemo;
bool vis[8][8];

void coloring(ull mask, int y, int x) {
	if((mask>>y*W+x&1)||vis[y][x]) return;
	vis[y][x]=true;
	rep(d, 4) coloring(mask,y+dxdys[d].first,x+dxdys[d].second);
}

bool dfs(const int y, const int x, const ull mask, const int t) {
	if(t == empties) return true;
	auto p = mp(mask,y*W+x);
	if(memo.count(p)) return memo[p];
	if(!badmemo.count(mask)) {
		mset(vis, 0); bool first=false;
		rep(i,H)rep(j,W)if(~mask>>i*W+j&1){
			if(!first) coloring(mask,i,j), first=true;
			else if(!vis[i][j]){
				return badmemo[mask] = false;
			}
		}
		badmemo[mask] = true;
	}else if(!badmemo[mask]) return false;

//	cout << y << ", " << x<< ", " << t << ": " << mask << endl;
	char perm[4]={0,1,2,3};
	random_shuffle(perm, perm+4);
	rep(d, 4) {
		int yy = y + dxdys[perm[d]].first, xx = x + dxdys[perm[d]].second;
		if(mask >> (yy * W + xx) & 1) continue;
		if(t < L-1) {
			char c = a[yy][xx];
			if(t == 0 && c == '2') continue;
			if('2' <= c && c < '0'+(L-t)) continue;
		}
		if(dfs(yy, xx, mask | (1ULL << (yy * W + xx)), t + 1))
			return memo[p] = true;
	}
	return memo[p] = false;
}

int main() {
	while(cin >> L >> H >> W, L) {
		rep(i, H) cin >> a[i];
		ull init = 0; int sy = -1, sx = -1;
		empties = 0;
		rep(i, H) rep(j, W) {
			if(a[i][j] == '#')
				init |= 1ULL << i * W + j;
			else if(a[i][j] == '1') {
				sy = i, sx = j;
				init |= 1ULL << i * W + j;
			}else empties ++;
		}
		memo.clear();
		cout << (dfs(sy, sx, init, 0) ? "Yes" : "No") << endl;
	}
	return 0;
}

Submission Info

Submission Time
Task I - 実績: ヘビマスター
User anta
Language C++11 (GCC 4.8.1)
Score 100
Code Size 3215 Byte
Status AC
Exec Time 52 ms
Memory 1936 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 16
Set Name Test Cases
All 00-sample, 01-maximum, 02-maximum, 03-maximum, 04-maximum, 05-maximum, 50-random01, 50-random02, 50-random03, 50-random04, 50-random05, 50-random06, 50-random07, 50-random08, 50-random09, 50-random10
Case Name Status Exec Time Memory
00-sample AC 21 ms 924 KB
01-maximum AC 28 ms 1012 KB
02-maximum AC 22 ms 928 KB
03-maximum AC 22 ms 932 KB
04-maximum AC 30 ms 924 KB
05-maximum AC 23 ms 928 KB
50-random01 AC 28 ms 1060 KB
50-random02 AC 24 ms 916 KB
50-random03 AC 30 ms 1128 KB
50-random04 AC 30 ms 1048 KB
50-random05 AC 28 ms 1064 KB
50-random06 AC 25 ms 844 KB
50-random07 AC 22 ms 924 KB
50-random08 AC 52 ms 1936 KB
50-random09 AC 27 ms 1044 KB
50-random10 AC 35 ms 1316 KB