#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;
char output[MAXSIZE+1];
char str[5]="NESW";
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(){
std::random_device rnd;
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 N=h1+w1;
vector <int> v (N);
rep(i,h1){
v[i]=2;
}
rep(j,w1){
v[h1+j]=1;
}
int globalbest =10000000;
for (int i=0;i<1000000;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]];
}
}
next_permutation(v.begin(), v.end());
}
for (int i=0;i<N;i++){
printf("%c",output[i]);
}
printf ("\n");
return 0;
}
Battle History