Submission #306130


Source Code Expand

#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <cctype>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rrep(i,n) for(int i = 1; i <= n; ++i)
#define drep(i,n) for(int i = n-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define snuke srand((unsigned)clock()+(unsigned)time(NULL))
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;

const int MX = 100005, INF = 1000010000;
const ll LINF = 1000000000000000000ll;
const double eps = 1e-10;
const int di[] = {-1,0,1,0}, dj[] = {0,-1,0,1}; //^<v>

// Max flow
struct Maxflow {
  int n;
  vector<int> to, lim, next, head, dist, it;
  Maxflow(){}
  Maxflow(int n):n(n),head(n,-1),it(n){}
  void addEdge(int a, int b, int c=1) {
    next.push_back(head[a]); head[a] = to.size(); to.push_back(b); lim.push_back(c);
    next.push_back(head[b]); head[b] = to.size(); to.push_back(a); lim.push_back(0); 
  }
  void addEdge2(int a, int b, int c=1) {
    next.push_back(head[a]); head[a] = to.size(); to.push_back(b); lim.push_back(c);
    next.push_back(head[b]); head[b] = to.size(); to.push_back(a); lim.push_back(c); 
  }
  void bfs(int sv){
    dist = vector<int>(n,INF);
    queue<int> q;
    dist[sv] = 0; q.push(sv);
    while(!q.empty()){
      int v = q.front(); q.pop();
      for(int i = head[v]; i != -1; i = next[i]) {
        if(lim[i] && dist[to[i]] == INF){
          dist[to[i]] = dist[v]+1; q.push(to[i]);
        }
      }
    }
  }
  int dfs(int v, int tv, int nf=INF){
    if(v == tv) return nf;
    for(; it[v] != -1; it[v] = next[it[v]]){
      int u = to[it[v]], f;
      if(!lim[it[v]] || dist[v] >= dist[u]) continue;
      if(f = dfs(u, tv, min(nf, lim[it[v]])), f){
        lim[it[v]] -= f;
        lim[it[v]^1] += f;
        return f;
      }
    }
    return 0;
  }
  int solve(int sv, int tv){
    int flow = 0, f;
    while(1){
      bfs(sv);
      if(dist[tv] == INF) return flow;
      rep(i,n) it[i] = head[i];
      while(f = dfs(sv,tv), f) flow += f;
    }
  }
  int nex(int v, int m) {
    for(int i = head[v]; i != -1; i = next[i]) {
      if(lim[i] && to[i] != v+m) return to[i];
    }
    puts("not found");
    return -1;
  }
};
//

int h, w;
string s[55];
int dist[55][55];

void bfss(int si, int sj) {
  queue<P> q;
  rep(i,h)rep(j,w) dist[i][j] = INF;
  dist[si][sj] = 0;
  q.push(P(si,sj));
  while(q.size()) {
    int i = q.front().fi, j = q.front().se; q.pop();
    rep(v,4) {
      int ni = i+di[v], nj = j+dj[v];
      if (ni<0||nj<0||ni>=h||nj>=w||s[ni][nj]=='#') continue;
      if (dist[ni][nj] != INF) continue;
      dist[ni][nj] = dist[i][j]+1;
      q.push(P(ni,nj));
    }
  }
}

int main(){
  cin >> h >> w;
  rep(i,h) cin >> s[i];
  int n = h*w, sv, tv = n*2, av, bv;
  rep(i,h)rep(j,w) if (s[i][j] == 'S') sv = i*w+j;
  rep(i,h)rep(j,w) if (s[i][j] == 'A') av = i*w+j;
  rep(i,h)rep(j,w) if (s[i][j] == 'B') bv = i*w+j;
  bfss(sv/w,sv%w);
  Maxflow mf(n*2+2);
  rep(i,n) {
    if (i == sv) mf.addEdge(i,n+i,2);
    else mf.addEdge(i,n+i);
  }
  rep(k,n) {
    int i = k/w, j = k%w;
    if (s[i][j] == '#') continue;
    rep(v,4) {
      int ni = i+di[v], nj = j+dj[v];
      if (ni<0||nj<0||ni>=h||nj>=w||s[ni][nj]=='#') continue;
      int nv = ni*w+nj;
      if (dist[i][j] < dist[ni][nj]) {
        mf.addEdge(n+k,nv);
      }
    }
  }
  //rep(i,h){rep(j,w) printf("%2d ",min(99,dist[i][j]));puts("");}
  mf.addEdge(n+av,tv);
  mf.addEdge(n+bv,tv);
  if (mf.solve(sv,tv) != 2) {
    puts("NA");
    return 0;
  }
  {
    int i = av/w, j = av%w;
    while(s[i][j] != 'S') {
      int v = i*w+j;
      int nx = mf.nex(v,n)-n;
      i = nx/w; j = nx%w;
      if (s[i][j] == 'S') break;
      s[i][j] = 'a';
    }
  }
  {
    int i = bv/w, j = bv%w;
    while(s[i][j] != 'S') {
      int v = i*w+j;
      int nx = mf.nex(v,n)-n;
      i = nx/w; j = nx%w;
      if (s[i][j] == 'S') break;
      s[i][j] = 'b';
    }
  }
  rep(i,h) cout << s[i] << endl; 
  return 0;
}





Submission Info

Submission Time
Task D - Maze
User snuke
Language C++11 (GCC 4.8.1)
Score 100
Code Size 4481 Byte
Status AC
Exec Time 35 ms
Memory 1188 KB

Judge Result

Set Name all
Score / Max Score 100 / 100
Status
AC × 41
Set Name Test Cases
all manual_j1.txt, manual_j10.txt, manual_j11.txt, manual_j12.txt, manual_j13.txt, manual_j14.txt, manual_j15.txt, manual_j2.txt, manual_j3.txt, manual_j4.txt, manual_j5.txt, manual_j6.txt, manual_j7.txt, manual_j8.txt, manual_j9.txt, manual_k1.txt, manual_k2.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_max_01.txt, random_max_02.txt, random_max_03.txt, random_max_04.txt, random_max_05.txt, random_max_06.txt, random_max_07.txt, random_max_08.txt, random_max_09.txt, random_max_10.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, white_01.txt, white_02.txt, white_03.txt, white_04.txt
Case Name Status Exec Time Memory
manual_j1.txt AC 22 ms 924 KB
manual_j10.txt AC 22 ms 800 KB
manual_j11.txt AC 25 ms 1184 KB
manual_j12.txt AC 35 ms 932 KB
manual_j13.txt AC 24 ms 928 KB
manual_j14.txt AC 30 ms 920 KB
manual_j15.txt AC 25 ms 920 KB
manual_j2.txt AC 25 ms 1056 KB
manual_j3.txt AC 25 ms 924 KB
manual_j4.txt AC 23 ms 932 KB
manual_j5.txt AC 27 ms 800 KB
manual_j6.txt AC 25 ms 916 KB
manual_j7.txt AC 25 ms 800 KB
manual_j8.txt AC 23 ms 920 KB
manual_j9.txt AC 23 ms 920 KB
manual_k1.txt AC 25 ms 1184 KB
manual_k2.txt AC 25 ms 1020 KB
random_01.txt AC 24 ms 1048 KB
random_02.txt AC 24 ms 1172 KB
random_03.txt AC 24 ms 988 KB
random_04.txt AC 23 ms 1056 KB
random_05.txt AC 23 ms 1052 KB
random_06.txt AC 24 ms 928 KB
random_max_01.txt AC 26 ms 1064 KB
random_max_02.txt AC 24 ms 1044 KB
random_max_03.txt AC 24 ms 988 KB
random_max_04.txt AC 24 ms 1064 KB
random_max_05.txt AC 26 ms 1188 KB
random_max_06.txt AC 24 ms 1052 KB
random_max_07.txt AC 26 ms 1060 KB
random_max_08.txt AC 25 ms 1068 KB
random_max_09.txt AC 24 ms 1064 KB
random_max_10.txt AC 24 ms 1176 KB
sample_01.txt AC 23 ms 736 KB
sample_02.txt AC 23 ms 928 KB
sample_03.txt AC 24 ms 924 KB
sample_04.txt AC 24 ms 780 KB
white_01.txt AC 24 ms 1176 KB
white_02.txt AC 26 ms 1172 KB
white_03.txt AC 26 ms 1016 KB
white_04.txt AC 25 ms 1176 KB