CSES - BAPC 2015 - Results
Submission details
Task:The King's Walk
Sender:Antti Röyskö
Submission time:2017-10-17 20:40:55 +0300
Language:C++
Status:READY
Result:
Test results
testverdicttime
#1--details

Code

#include <iostream>
#include <vector>
#define F first
#define S second
#define MP make_pair
using namespace std;
int T;
int N;

const int M=5318008;

short ki[5050][5050];
short d[5050][5050];


pair<int, int> a[20111];
pair<int, int> b[20111];

pair<int, int>* l;
pair<int, int>* n;

int lpos;
int npos;

inline void add(int x, int y, int kk, int dd){
  if (x<1 || y<1 || x>N || y>N) return;
  if (d[x][y]<dd) return;
  if (d[x][y]!=dd){
    d[x][y]=dd;
    ki[x][y]=npos;
    n[npos++]=(MP(x|(y<<16), 0));
  }
  n[ki[x][y]].S+=kk;
}

int main(){
  ios_base::sync_with_stdio(0); cin.tie(0);
  cin >> T;
  
  l=a;
  n=b;
  
  for (int i=0; i<T; ++i){
    cin >> N;
    for (int i=1; i<=N; ++i) for (int j=1; j<=N; ++j){d[i][j]=20000;}
    lpos=0;
    npos=0;
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    add(x1, y1, 1, 0);
    bool ans=0;
    int ansv=0;
    while (!ans){
      swap(l, n);
      swap(lpos, npos);
      npos=0;
      for (int i=0; i<lpos; ++i){
	int x=(l[i].F)&((1<<16)-1);
	int y=(l[i].F)>>16;
	int kk=l[i].S;
	kk%=M;
	if (x==x2 && y==y2){
	  ans=1;
	  ansv=kk;
	  break;
	}
	add(x-1, y-1, kk, d[x][y]+1);
	add(x-1, y, kk, d[x][y]+1);
	add(x-1, y+1, kk, d[x][y]+1);
	add(x, y-1, kk, d[x][y]+1);
	add(x, y+1, kk, d[x][y]+1);
	add(x+1, y-1, kk, d[x][y]+1);
	add(x+1, y, kk, d[x][y]+1);
	add(x+1, y+1, kk, d[x][y]+1);
      }
    }
    cout << ansv << "\n";
    
  }
}

Test details

Test 1

Verdict:

input
100
315
152 73 251 114
3201
2894 304 697 2576
...

correct output
898608
4727344
240176
248812
1744064
...

user output
(empty)