#define H 50
#define W 50
#define MAXSIZE 100
#include <random>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) ((x).begin(),(x).end())
using ll = long long;
typedef long long ll;
typedef long long int64;
typedef long long lint;
typedef long long lli;
int gridlist[H][W];
int flg[H][W];
int v[MAXSIZE];
using namespace std;
random_device rnd;
char output[MAXSIZE+1];
char str[5]="NESW";
void shuffle(vector <int> array, int size) {
for(int i = 0; i < size; i++) {
int j = rnd()%size;
int t = array[i];
array[i] = array[j];
array[j] = t;
}
}
int search(vector <int> v,int N){
int score =gridlist[0][0];
int posx=0,posy=0;
for (int i=0;i<N;i++){
switch (v[i]){
case 0:
posy-=1;
break;
case 1:
posx+=1;
break;
case 2:
posy+=1;
break;
case 3:
posx-=1;
break;
}
score+=gridlist[posx][posy];
}
return score;
}
int main(){
int h,w;
scanf("%d %d",&h,&w);
rep(j,h)
{
rep(i,w){
scanf("%d ",&gridlist[j][i]);
}
}
rep(j,h){
rep(i,w){
}
}
int h1=h-1;
int w1=w-1;
int OPTION=0;
int sabunh1;
int sabunw1;
if (OPTION){
int tmph1=h1,tmpw1=w1;
if (h1>10){
h1=10;
}
if (w1>10){
w1=10;
}
sabunh1=tmph1-h1;
sabunw1=tmpw1-w1;
}
int N=h1+w1;
vector <int> v (N);
rep(i,h1){
v[i]=2;
}
rep(j,w1){
v[h1+j]=1;
}
int globalbest =10000000;
int step=100000000/(h1+w1);
for (int i=0;i<step;i++){
int score=search(v,N);
if (score<0)score*=-1;
if (score<globalbest){
globalbest=score;
for (int i=0;i<N;i++){
output[i]=str[v[i]];
}
}
shuffle(v,N);
}
if(OPTION){
for (int i=0;i<sabunh1;i++){
printf("S");
}
for (int i=0;i<sabunw1;i++){
printf("E");
}
for (int i=0;i<N;i++){
printf("%c",output[i]);
}
printf ("\n");
} else{
for (int i=0;i<N;i++){
printf("%c",output[i]);
}
printf ("\n");
}
return 0;
}
Battle History