Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

multidimensional array - C++ matrix path to exit

Navigating a matrix, Given a matrix output path to the exit Start from '*' go through '.' but not '#' finish at 'x'. You can only move North, South, East, West

Input:

4 4
* . # #
# . . #
# # . #
x . . #

Output:

ESESSWW

Code:

int cnt=0, x,y,n,m; cin>>n>>m;  char c[n][m]; int cc[n][m]={}; bool found=false;
for(int i=0;i<n;i++)
    for(int j=0;j<m;j++) {cin>>c[i][j];  if(c[i][j]=='*'){x=i; y=j;} }

while(!found){  
    ++x; if(c[x][y]!='#' && cc[x][y]==0) {if(c[x][y]=='x') found=true; cout<<"N"; cc[--x][y]++; } 
    --x; if(c[x][y]!='#' && cc[x][y]==0) {if(c[x][y]=='x') found=true; cout<<"S"; cc[++x][y]++; }
    ++y; if(c[x][y]!='#' && cc[x][y]==0) {if(c[x][y]=='x') found=true; cout<<"E"; cc[x][--y]++; }
     --y; if(c[x][y]!='#' && cc[x][y]==0) {if(c[x][y]=='x') found=true; cout<<"W"; cc[x][++y]++; }  
}

My methodology is: start from '*' look around the element is there is a '.' and check if that '.' was never visited, if yes move to that and repeat. My way is clearly wrong and I cannot think of a better way to approach it, I would appreciate any suggestions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...