Problem:
Problem F:
Line Puzzle
プログラミングでパズルを解いてみよう。
n
×
n
の数字が格子状に並んでいる。数字のいくつかは丸で囲まれており、これらを起点と呼ぶことにする。パズルのルールは以下のとおりである：
各起点から縦・横に進む１本の線を引く（斜めには引けない）。
通った数字の和が起点の数字と同じになるように線を伸ばす。
線は枝分かれしてはいけない。
線はすでに引かれた数字を通ることはできない（線は交差してはいけない）。
線は２個以上の起点を通ることはできない。
下図に示すように、パズルのゴールはすべての起点を使い、すべての数字に線を引くことである。
あなたの仕事は、パズルを解くプログラムを作成することである。
ただしこの問題では、与えられたパズルが解けるものかどうかを判定するだけでよい。
Input
入力は複数のデータセットからなる。各データセットの形式は以下のとおりである：
n
n
×
n
の数字
n
×
n
の数字が与えられるパズルを示し、起点の数字は負の数として与えられる。
n
が 0 のとき入力の終わりを示す。
n
は 3 以上 8 以下、起点以外の数字は 1 以上 50 以下、起点は -50 以上 -1 以下であると仮定してよい。また、入力されるパズルの性質として以下のことを仮定してよい：
与えられるパズルの各行、各列には最低でも１つの起点がある。
起点の個数は、全体の数字の個数 (
n
×
n
) の 20 % から 40 % 程度である。
Output
各データセットに対して、パズルが解けるものであれば "YES" を、そうでなければ "NO" と１行に出力せよ。
Sample Input
3
-3 1 1
2 -4 1
2 1 -1
3
-4 1 1
1 1 -6
1 -5 3
4
-8 6 -2 1
2 -7 -2 1
1 -1 1 1
1 1 1 -5
6
2 2 3 -7 3 2
1 -10 1 1 3 2
2 6 5 2 -6 1
3 4 -23 2 2 5
3 3 -6 2 3 7
-7 2 3 2 -5 -13
6
2 2 3 -7 3 2
1 -10 1 1 3 2
2 6 5 2 -6 1
3 4 -23 2 2 5
3 3 -6 2 3 7
-7 2 3 2 -5 -12
0
Output for the Sample Input
YES
NO
NO
YES
NO

Submitted solutions:

code_snippet: #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);

bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];

    if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[ny][nx]) {
      used[ny][nx] = true;
      if (dfs(cur, sum + mat[ny][nx], nx, ny))
        return true;
      used[ny][nx] = false;
    }
  }
  return false;
}

void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();
  int sum = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }

  bool ret = false;

  if (sum != 0) {
    ret = false;
  } else if (starts.size() == 1) {
    ret = true;
  } else {
    sort(starts.begin(), starts.end(), greater<SP>());
    ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
  }

  cout << (ret ? "YES" : "NO") << endl;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  while (true) {
    cin >> n;
    if (n == 0)
      break;
    solve();
  }

  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3152
final_rank: 9

code_snippet: #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);
bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[ny][nx]) {
      used[ny][nx] = true;
      if (dfs(cur, sum + mat[ny][nx], nx, ny))
        return true;
      used[ny][nx] = false;
    }
  }
  return false;
}
void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      scanf("%d", &a);
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }
  bool ret = false;
  if (sum != 0) {
    ret = false;
  } else if (starts.size() == 1) {
    ret = true;
  } else {
    sort(starts.begin(), starts.end(), greater<SP>());
    ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
  }
  puts(ret ? "YES" : "NO");
  return;
}
int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3516
final_rank: 17

code_snippet: #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);
bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[ny][nx]) {
      used[ny][nx] = true;
      if (dfs(cur, sum + mat[ny][nx], nx, ny))
        return true;
      used[ny][nx] = false;
    }
  }
  return false;
}
void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      scanf("%d", &a);
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }
  bool ret = false;
  if (sum != 0) {
    ret = false;
  } else if (starts.size() == 1) {
    ret = true;
  } else {
    sort(starts.begin(), starts.end(), greater<SP>());
    ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
  }
  printf("%s\n", ret ? "YES" : "NO");
  return;
}
int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3192
final_rank: 10

code_snippet: #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);
bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < n && ny >= 0 && ny < n && !used[ny][nx]) {
      used[ny][nx] = true;
      if (dfs(cur, sum + mat[ny][nx], nx, ny))
        return true;
      used[ny][nx] = false;
    }
  }
  return false;
}
void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }
  bool ret = false;
  if (sum == 0 && starts.size() > 1) {
    sort(starts.begin(), starts.end(), greater<SP>());
    ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
  } else if (starts.size() == 1) {
    ret = true;
  }
  cout << (ret ? "YES" : "NO") << endl;
  return;
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3132
final_rank: 8

code_snippet: #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);

bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < n && ny >= 0 && ny < n && !used[ny][nx]) {
      used[ny][nx] = true;
      if (dfs(cur, sum + mat[ny][nx], nx, ny))
        return true;
      used[ny][nx] = false;
    }
  }

  return false;
}

void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();

  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }

  bool ret = false;
  if (sum != 0) {
    ret = false;
  } else if (starts.size() == 1) {
    ret = true;
  } else {
    sort(starts.begin(), starts.end(), greater<SP>());
    ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
  }

  cout << (ret ? "YES" : "NO") << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  while (true) {
    cin >> n;
    if (n == 0) break;

    solve();
  }

  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3304
final_rank: 12

code_snippet: #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);
bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[ny][nx]) {
      used[ny][nx] = true;
      if (dfs(cur, sum + mat[ny][nx], nx, ny))
        return true;
      used[ny][nx] = false;
    }
  }
  return false;
}
void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      scanf("%d", &a);
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }
  bool ret = false;
  if (sum == 0) {
    if (starts.size() == 1) {
      ret = true;
    } else {
      sort(starts.begin(), starts.end(), greater<SP>());
      ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
    }
  }
  cout << (ret ? "YES" : "NO") << endl;
  return;
}
int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3268
final_rank: 11

code_snippet: #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);
bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[ny][nx]) {
      used[ny][nx] = true;
      if (dfs(cur, sum + mat[ny][nx], nx, ny))
        return true;
      used[ny][nx] = false;
    }
  }
  return false;
}
void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      scanf("%d", &a);
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }
  bool ret = false;
  if (sum != 0) {
    ret = false;
  } else if (starts.size() == 1) {
    ret = true;
  } else {
    sort(starts.begin(), starts.end(), greater<SP>());
    ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
  }
  printf("%s\n", ret ? "YES" : "NO");
  return;
}
int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3396
final_rank: 14

code_snippet: #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);

bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[ny][nx]) {
      used[ny][nx] = true;
      if (dfs(cur, sum + mat[ny][nx], nx, ny))
        return true;
      used[ny][nx] = false;
    }
  }
  return false;
}

void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }
  bool ret = false;
  if (sum != 0) {
    ret = false;
  } else if (starts.size() == 1) {
    ret = true;
  } else {
    sort(starts.begin(), starts.end(), greater<SP>());
    ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
  }
  cout << (ret ? "YES" : "NO") << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  while (true) {
    cin >> n;
    if (n == 0)
      break;
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3356
final_rank: 13

code_snippet: #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);
bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || nx >= n || ny < 0 || ny >= n || used[ny][nx]) continue;
    used[ny][nx] = true;
    if (dfs(cur, sum + mat[ny][nx], nx, ny)) return true;
    used[ny][nx] = false;
  }
  return false;
}
void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }
  bool ret = false;
  if (sum == 0 && starts.size() != 1) {
    sort(starts.begin(), starts.end(), greater<SP>());
    ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
  }
  cout << (ret ? "YES" : "NO") << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  while (cin >> n && n) solve();
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3400
final_rank: 15

code_snippet: #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);
bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[ny][nx]) {
      used[ny][nx] = true;
      if (dfs(cur, sum + mat[ny][nx], nx, ny))
        return true;
      used[ny][nx] = false;
    }
  }
  return false;
}
void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }
  bool ret = false;
  ;
  if (sum != 0) {
    ret = false;
  } else if (starts.size() == 1) {
    ret = true;
  } else {
    sort(starts.begin(), starts.end(), greater<SP>());
    ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
  }
  cout << (ret ? "YES" : "NO") << endl;
  return;
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3456
final_rank: 16

code_snippet: #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> Point;
typedef pair<int, Point> SP;
typedef vector<vector<int>> Mat;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int n;
Mat mat;
vector<vector<bool>> used;
vector<SP> starts(0);
bool dfs(int cur, int sum, int x, int y) {
  if (starts[cur].first == sum) {
    cur++;
    sum = 0;
    if (cur >= starts.size()) {
      return true;
    }
    x = starts[cur].second.second;
    y = starts[cur].second.first;
  } else if (starts[cur].first < sum) {
    return false;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[ny][nx]) {
      used[ny][nx] = true;
      if (dfs(cur, sum + mat[ny][nx], nx, ny))
        return true;
      used[ny][nx] = false;
    }
  }
  return false;
}
void solve() {
  mat = Mat(n, vector<int>(n));
  used = vector<vector<bool>>(n, vector<bool>(n, false));
  starts.clear();
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      sum += a;
      if (a < 0) {
        a *= -1;
        used[i][j] = true;
        starts.push_back(SP(a, Point(i, j)));
      }
      mat[i][j] = a;
    }
  }
  bool ret = false;
  ;
  if (sum != 0) {
    ret = false;
  } else if (starts.size() == 1) {
    ret = true;
  } else {
    ret = dfs(0, 0, starts[0].second.second, starts[0].second.first);
  }
  cout << (ret ? "YES" : "NO") << endl;
  return;
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3376
final_rank: 6

code_snippet: #include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int n;
int t[10][10];
long long v;
map<long long, bool> m;
vector<P> can;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};
bool rec(int id);
bool dfs(int id, int y, int x, int sum) {
  if (sum == 0)
    return rec(id + 1);
  if (sum > 0)
    return false;
  for (int i = 0; i < 4; i++) {
    int ty = y + dy[i], tx = x + dx[i];
    if (ty < 0 || tx < 0 || n <= ty || n <= tx)
      continue;
    if ((v >> (ty * n + tx)) & 1LL || t[ty][tx] < 0)
      continue;
    v |= 1LL << (ty * n + tx);
    if (dfs(id, ty, tx, sum + t[ty][tx]))
      return true;
    v -= 1LL << (ty * n + tx);
  }
  return false;
}
bool rec(int id) {
  if (id == can.size())
    return true;
  if (m[v])
    return false;
  m[v] = true;
  int y = can[id].first, x = can[id].second;
  v |= 1LL << y * n + x;
  return dfs(id, y, x, t[y][x]);
}
int main() {
  while (cin >> n, n) {
    int sum = 0;
    can.clear();
    m.clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cin >> t[i][j];
        sum += t[i][j];
        if (t[i][j] < 0)
          can.push_back(P(i, j));
      }
    v = 0LL;
    cout << ((!sum && rec(0)) ? "YES\n" : "NO\n");
  }
}
accuracy: 1
time_ms: 30
memory_kb: 6200
final_rank: 20

code_snippet: #include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int n;
int t[10][10];
long long v;
map<long long, bool> m;
vector<P> can;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};
bool rec(int id);
bool dfs(int id, int y, int x, int sum) {
  if (sum == 0)
    return rec(id + 1);
  if (sum > 0)
    return false;
  for (int i = 0; i < 4; i++) {
    int ty = y + dy[i], tx = x + dx[i];
    if (ty < 0 || tx < 0 || n <= ty || n <= tx)
      continue;
    if ((v >> (ty * n + tx)) & 1LL || t[ty][tx] < 0)
      continue;
    v |= 1LL << (ty * n + tx);
    if (dfs(id, ty, tx, sum + t[ty][tx]))
      return true;
    v &= ~(1LL << (ty * n + tx));
  }
  return false;
}
bool rec(int id) {
  if (id == can.size())
    return true;
  if (m[v])
    return false;
  m[v] = true;
  int y = can[id].first, x = can[id].second;
  return dfs(id, y, x, t[y][x]);
}
int main() {
  while (cin >> n, n) {
    int sum = 0;
    can.clear();
    m.clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cin >> t[i][j];
        sum += t[i][j];
        if (t[i][j] < 0) {
          can.push_back(P(i, j));
          v |= 1LL << (i * n + j);
        }
      }
    v = 0LL;
    cout << ((!sum && rec(0)) ? "YES\n" : "NO\n");
  }
}
accuracy: 1
time_ms: 30
memory_kb: 6168
final_rank: 19

code_snippet: #include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int tb[10][10];
bool visited[10][10];
int n, t, sum;
vector<int> sx, sy;
bool dfs(int x, int y, int s, int c, int m) {
  int tx, ty, ttx, tty, tc, tm, ts;
  for (int i = 0; i < 4; i++) {
    bool v = false;
    ts = s;
    tc = c;
    tx = x + dx[i];
    ty = y + dy[i];
    ttx = x;
    tty = y;
    tm = m + tb[tx][ty];
    if (tb[tx][ty] <= 0)
      continue;
    if (visited[tx][ty])
      continue;
    if (tm > 0)
      continue;
    if (tm == 0) {
      tc++;
      ts++;
      if (tc == sx.size()) {
        if (ts == n * n) {
          return true;
        } else {
          continue;
        }
      }
      ttx = tx;
      tty = ty;
      tx = sx[tc];
      ty = sy[tc];
      tm = tb[tx][ty];
    }
    visited[ttx][tty] = 1;
    visited[x][y] = 1;
    if (dfs(tx, ty, ts + 1, tc, tm))
      return true;
    visited[x][y] = 0;
    visited[ttx][tty] = 0;
  }
  return false;
}
int main() {
  while (cin >> n, n) {
    sum = 0;
    memset(visited, 0, sizeof(visited));
    sx.clear();
    sy.clear();
    for (int i = 0; i < 100; i++) {
      tb[i / 10][i % 10] = 0;
    }
    for (int i = 0; i < n * n; i++) {
      cin >> t;
      sum += t;
      tb[(i / n) + 1][(i % n) + 1] = t;
      if (t < 0) {
        sx.push_back((i / n) + 1);
        sy.push_back((i % n) + 1);
      }
    }
    if (sum != 0) {
      cout << "NO\n";
      continue;
    }
    if (dfs(sx[0], sy[0], 1, 0, tb[sx[0]][sy[0]])) {
      cout << "YES\n";
    } else {
      cout << "NO\n";
    }
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3024
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
using namespace std;
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int tb[10][10];
int tf[10][10];
int n, t, sum;
vector<int> sx, sy;
bool dfs(int x, int y, int s, int c, int m) {
  int tx, ty, ts, tc, tm, f;
  for (int i = 0; i < 4; i++) {
    f = false;
    tc = c;
    ts = s + 1;
    tx = x + dx[i];
    ty = y + dy[i];
    tm = m + tb[tx][ty];
    if (tf[tx][ty] == 0)
      continue;
    if (tb[tx][ty] <= 0)
      continue;
    if (tm > 0)
      continue;
    if (tm == 0) {
      tc++;
      if (tc == sx.size()) {
        if (ts == n * n) {
          return true;
        } else {
          return false;
        }
      }
      ts++;
      tx = sx[tc];
      ty = sy[tc];
      tm = tb[tx][ty];
      f = true;
      tf[x + dx[i]][y + dy[i]] = 0;
    }
    tf[x][y] = 0;
    if (dfs(tx, ty, ts, tc, tm))
      return true;
    tf[x][y] = 1;
    if (f) {
      tf[x + dx[i]][y + dy[i]] = 1;
    }
  }
  return false;
}
int main() {
  while (cin >> n, n) {
    sum = 0;
    sx.clear();
    sy.clear();
    for (int i = 0; i < 100; i++) {
      tb[i / 10][i % 10] = 0;
      tf[i / 10][i % 10] = 1;
    }
    for (int i = 0; i < n * n; i++) {
      cin >> t;
      sum += t;
      tb[(i / n) + 1][(i % n) + 1] = t;
      if (t < 0) {
        sx.push_back((i / n) + 1);
        sy.push_back((i % n) + 1);
      }
    }
    if (sum != 0) {
      cout << "NO\n";
      continue;
    }
    if (dfs(sx[0], sy[0], 1, 0, tb[sx[0]][sy[0]])) {
      cout << "YES\n";
    } else {
      cout << "NO\n";
    }
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 2944
final_rank: 3

code_snippet: #include <iostream>
using namespace std;
const int dy[] = {0, 1, -1, 0}, dx[] = {1, 0, 0, -1};
int n, mas[8][8];
bool used[8][8];
bool isover(int &y, int &x) { return x < 0 || y < 0 || x >= n || y >= n; }
bool dfs(int hoge, int y, int x, int sum) {
  if (hoge == 0 && sum == n * n)
    return true;
  bool ret = false;
  if (hoge < 0) {
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (!isover(ny, nx) && mas[ny][nx] > 0 && !used[ny][nx]) {
        used[ny][nx] = true;
        ret += dfs(hoge + mas[ny][nx], ny, nx, sum + 1);
        used[ny][nx] = false;
      }
    }
  } else if (!hoge) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (mas[i][j] < 0 && !used[i][j]) {
          used[i][j] = true;
          ret += dfs(mas[i][j], i, j, sum + 1);
          used[i][j] = false;
          return ret;
        }
      }
    }
  }
  return ret;
}
int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> mas[i][j];
      }
    }
    cout << (dfs(0, 0, 0, 0) ? "YES" : "NO") << endl;
  }
}
accuracy: 1
time_ms: 40
memory_kb: 3008
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (int i = 0; i < n; i++)
bool dfs(int, int);
bool dfs2(int, int, const int, const int, int);
int n, number[8][8];
const int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};
bool dfs2(int x, int y, const int basex, const int basey, int sub) {
  if (sub < 0)
    return false;
  if (sub == 0)
    return dfs(basex + 1, basey);
  Rep(i, 4) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny < 0 || ny >= n || nx < 0 || nx >= n || number[ny][nx] <= 0)
      continue;
    int next = number[ny][nx];
    number[ny][nx] = 0;
    if (dfs2(nx, ny, basex, basey, sub - next))
      return true;
    number[ny][nx] = next;
  }
  return false;
}
bool dfs(int x, int y) {
  if (y == n) {
    Rep(i, n) Rep(j, n) if (number[i][j] != 0) return false;
    return true;
  }
  if (x == n)
    return dfs(0, y + 1);
  if (number[y][x] >= 0)
    return dfs(x + 1, y);
  else {
    int prev = number[y][x];
    number[y][x] = 0;
    if (dfs2(x, y, x, y, -prev))
      return true;
    number[y][x] = prev;
  }
  return false;
}
int main() {
  while (cin >> n, n) {
    int ret = 0;
    Rep(i, n) {
      Rep(j, n) {
        cin >> number[i][j];
        ret += number[i][j];
      }
    }
    if (ret == 0 && dfs(0, 0))
      puts("YES");
    else
      puts("NO");
  }
}
accuracy: 1
time_ms: 20
memory_kb: 2920
final_rank: 2

code_snippet: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
struct P {
  int x, y;
  P(int x, int y) : x(x), y(y) {}
};
int num[10][10];
bool visited[10][10];
std::vector<P> v;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
bool BackTrack(int x, int y, int index, int rest) {
  if (rest == 0) {
    if (index == v.size() - 1)
      return true;
    ++index;
    x = v[index].x;
    y = v[index].y;
    rest = -num[y][x];
    return BackTrack(x, y, index, rest);
  }
  bool res = false;
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (visited[ny][nx])
      continue;
    visited[ny][nx] = true;
    res = res || BackTrack(nx, ny, index, rest - num[ny][nx]);
    visited[ny][nx] = false;
  }
  return res;
}
int main() {
  int n, i, j, t;
  while (scanf("%d", &n), n) {
    v.clear();
    memset(visited, 1, sizeof(visited));
    int check = 0;
    for (i = 1; i <= n; ++i) {
      for (j = 1; j <= n; ++j) {
        scanf("%d", &t);
        num[i][j] = t;
        if (t < 0) {
          v.push_back(P(j, i));
          visited[i][j] = true;
        } else {
          visited[i][j] = false;
        }
        check += t;
      }
    }
    int x = v[0].x;
    int y = v[0].y;
    puts(check == 0 && BackTrack(x, y, 0, -num[y][x]) ? "YES" : "NO");
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 2588
final_rank: 18

code_snippet: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
struct P {
  int x, y;
  P(int x, int y) : x(x), y(y) {}
};
int num[10][10];
bool visited[10][10];
std::vector<P> v;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
bool BackTrack(int x, int y, int index, int rest) {
  if (rest < 0)
    return false;
  if (rest == 0) {
    if (index == v.size() - 1)
      return true;
    ++index;
    x = v[index].x;
    y = v[index].y;
    rest = -num[y][x];
    return BackTrack(x, y, index, rest);
  }
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (visited[ny][nx])
      continue;
    visited[ny][nx] = true;
    if (BackTrack(nx, ny, index, rest - num[ny][nx]))
      return true;
    visited[ny][nx] = false;
  }
  return false;
}
int main() {
  int n, i, j, t;
  while (scanf("%d", &n), n) {
    v.clear();
    memset(visited, 1, sizeof(visited));
    int check = 0;
    for (i = 1; i <= n; ++i) {
      for (j = 1; j <= n; ++j) {
        scanf("%d", &t);
        num[i][j] = t;
        if (t < 0) {
          v.push_back(P(j, i));
          visited[i][j] = true;
        } else {
          visited[i][j] = false;
        }
        check += t;
      }
    }
    int x = v[0].x;
    int y = v[0].y;
    puts(check == 0 && BackTrack(x, y, 0, -num[y][x]) ? "YES" : "NO");
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2520
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;


int N,table[8][8];
int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};
bool FLG,check[8][8];

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= N-1 && col >= 0 && col <= N-1)return true;
	else{
		return false;
	}
}

void dfs(int sum,int tmp_row,int tmp_col,int rest){

	if(FLG) return;

	if(sum == 0 && rest == 0){
		FLG = true;
		return;
	}

	if(sum == 0){

		for(int row = 0; row < N; row++){
			for(int col = 0; col < N; col++){
				if(check[row][col])continue;
				if(table[row][col] < 0){
					check[row][col] = true;
					dfs(abs(table[row][col]),row,col,rest-1);
					check[row][col] = false;
					return;
				}
			}
		}

	  }else{

		  for(int i = 0; i < 4; i++){
			  int adj_row = tmp_row+diff_row[i];
			  int adj_col = tmp_col+diff_col[i];

			  if(rangeCheck(adj_row,adj_col) == false || check[adj_row][adj_col] == true || table[adj_row][adj_col] < 0 || sum < table[adj_row][adj_col])continue;

			  check[adj_row][adj_col] = true;
			  dfs(sum-table[adj_row][adj_col],adj_row,adj_col,rest-1);
			  check[adj_row][adj_col] = false;
		  }
	  }
}


void func(){

	for(int row = 0; row < N; row++){
		for(int col = 0; col < N; col++){
			scanf("%d",&table[row][col]);
			check[row][col] = false;
		}
	}

	FLG = false;

	dfs(0,0,0,N*N);

	if(FLG){
		printf("YES\n");
	}else{
		printf("NO\n");
	}
}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3008
final_rank: 4