Artifact Content
Not logged in

Artifact 56c161848bbee8593bf02d06f271031b5f255c97


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class VisitALot { public:
	string travel(int R, int C, vector <int> obsr, vector <int> obsc)
	{
		if (R >= 6) {
			string ans;
			int r = 0, c = 0;
			while (r < R) {
				if (count(obsr.begin(), obsr.end(), r) == 0) {
					if (c == 0)
						while (c + 1 < C) {
							ans += 'E';
							c++;
						}
					else
						while (c - 1 >= 0) {
							ans += 'W';
							c--;
						}
				}

				if (r + 1 < R)
					ans += 'S';
				r++;
			}
			return ans;
		}

		if (C >= 6) {
			string ans;
			int r = 0, c = 0;
			while (c < C) {
				if (count(obsc.begin(), obsc.end(), c) == 0) {
					if (r == 0)
						while (r + 1 < R) {
							ans += 'S';
							r++;
						}
					else
						while (r - 1 >= 0) {
							ans += 'N';
							r--;
						}
				}

				if (c + 1 < C)
					ans += 'E';
				c++;
			}
			return ans;
		}

		// naive search
		vector<vector<bool>> visited(R, vector<bool>(C));
		for (int i = 0; i < obsr.size(); ++i)
			visited[obsr[i]][obsc[i]] = true;
		visited[0][0] = true;
		string ans;
		dfs(ans, R, C, 0, 0, visited);
		return ans;
	}

	bool dfs(string& s, int R, int C, int r, int c, vector<vector<bool>>& visited) {
		int dr[] = { -1,+1,0,0 };
		int dc[] = { 0,0,-1,+1 };
		char dd[] = { 'N', 'S', 'W', 'E' };
		for (int i = 0; i < 4; ++i) {
			int rr = r + dr[i];
			int cc = c + dc[i];
			if (0 <= rr && rr < R && 0 <= cc && cc < C && !visited[rr][cc]) {
				visited[rr][cc] = true;
				s += dd[i];
				if (s.size()+1 >= (R*C+1)/2 || dfs(s, R, C, rr, cc, visited))
					return true;
				s.resize(s.size() - 1);
				visited[rr][cc] = false;
			}
		}
		return false;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, VisitALot().travel(R, C, obsr, obsc));}
int main(){

CASE(0)
	int R = 2; 
	int C = 3; 
	vector <int> obsr; 
	vector <int> obsc; 
	string _ = "SENE"; 
END
CASE(1)
	int R = 6; 
	int C = 5; 
	int obsr_[] = {1, 1, 4};
	  vector <int> obsr(obsr_, obsr_+sizeof(obsr_)/sizeof(*obsr_)); 
	int obsc_[] = {1, 3, 1};
	  vector <int> obsc(obsc_, obsc_+sizeof(obsc_)/sizeof(*obsc_)); 
	string _ = "SSEESWWSSEENEE"; 
END
CASE(2)
	int R = 7; 
	int C = 8; 
	vector <int> obsr; 
	vector <int> obsc; 
	string _ = "EEEEEEESWWWWWWWSEEEEEEESWWWWWWWSEEEEEEESWWWWWWWSEEEEEEE"; 
END
CASE(3)
	int R = 7; 
	int C = 4; 
	int obsr_[] = { 5,2,1 };
	  vector <int> obsr(obsr_, obsr_+sizeof(obsr_)/sizeof(*obsr_)); 
	  int obsc_[] = { 1,2,2 };
	  vector <int> obsc(obsc_, obsc_+sizeof(obsc_)/sizeof(*obsc_)); 
	string _ = "?"; 
END
CASE(4)
	int R = 50; 
	int C = 50; 
	int obsr_[] = { 1,10,15 };
	  vector <int> obsr(obsr_, obsr_+sizeof(obsr_)/sizeof(*obsr_)); 
	  int obsc_[] = { 28, 17, 3 };
	  vector <int> obsc(obsc_, obsc_+sizeof(obsc_)/sizeof(*obsc_)); 
	string _ = "?"; 
END
CASE(4)
int R = 5;
int C = 5;
int obsr_[] = { 1,2,3 };
vector <int> obsr(obsr_, obsr_ + sizeof(obsr_) / sizeof(*obsr_));
int obsc_[] = { 1,2,3 };
vector <int> obsc(obsc_, obsc_ + sizeof(obsc_) / sizeof(*obsc_));
string _ = "?";
END
CASE(4)
int R = 4;
int C = 4;
int obsr_[] = { 1,2 };
vector <int> obsr(obsr_, obsr_ + sizeof(obsr_) / sizeof(*obsr_));
int obsc_[] = { 1,2 };
vector <int> obsc(obsc_, obsc_ + sizeof(obsc_) / sizeof(*obsc_));
string _ = "?";
END
}
// END CUT HERE