SRM 523 Div2 Easy AlphabetPath

問題

2次元の文字列が与えられた時上下左右を'A'から辿って,'B', 'C', ..., 'Z'と辿れるか判定せよ.

解答

やるだけですな.

class AlphabetPath {
public:
  string doesItExist(vector <string> letterMaze) {
    string res;
    int n = letterMaze.size();
    int m = letterMaze[0].size();
    int i, j;
    bool ok = true;
    // cout << "n=" << n << " m=" << m << endl;
    for(char c = 'A'; c < 'Z'; c++){
      for(i = 0; i < n; i++){
        for(j = 0; j < m; j++) if (letterMaze[i][j] == c){
            bool check = false;
            // cout << "i=" << i << " j=" << j << endl;
            if(i-1 >= 0 && letterMaze[i-1][j] == c+1) check = true;
            if(i+1 < n && letterMaze[i+1][j] == c+1) check = true;
            if(j-1 >= 0 && letterMaze[i][j-1] == c+1) check = true;
            if(j+1 < m && letterMaze[i][j+1] == c+1) check = true;
            ok &= check;
            if(!ok) return "NO";
          }
      }
    }
    return "YES";

	
    return res;
  }