DMOPC '13 Contest 1 P4 - AFK
Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef queue<int> qi;
typedef queue<char> qc;
typedef stack<int> si;
typedef stack<char> sc;
#define f first
#define s second
#define pb push_back
char raw[55][55];
int vis[55][55];
int l, w;
int sii, sj;
int di[4] = {0,1,0,-1};
int dj[4] = {1,0,-1,0};
int main()
{
int T;
cin >> T;
while (T--)
{
memset(vis, -1, sizeof vis);
cin >> l >> w;
string in;
for (int i = 0 ; i < w; i++)
{
cin>>in;
for (int j = 0; j <l; j++)
{
raw[i][j] = in[j];
if (raw[i][j] == 'C'){sii = i; sj = j;}
}
}
queue<pii> q;
pii current = pii(sii,sj);
q.push(current);
vis[sii][sj] = 0;
int ci, cj;
bool worth = false;
while(!q.empty())
{
current = q.front();
q.pop();
ci = current.first; cj = current.second;
if (raw[ci][cj] =='W')
{
worth = true;
if (vis[ci][cj] >= 60) {
cout << "#notworth\n";
}else{
cout << vis[ci][cj] << "\n";
break;
}
}
for (int i = 0 ; i < 4; i++)
{
ci = current.first+di[i]; cj = current.second +dj[i];
if (ci < 0 || cj < 0 || ci >= w || cj >= l) continue;
if (raw[ci][cj] =='X' || vis[ci][cj] != -1) continue;
vis[ci][cj] = vis[current.first][current.second]+ 1;
q.push(pii(ci,cj));
}
}
if (!worth)
cout<<"#notworth\n";
}
}