Sha 639 Don't Get Rooked deformation N queen problem brute force backtracking

Source: Internet
Author: User

Question: like Queen N, we do not consider diagonal conflicts, but consider the existence of walls, as long as there is a wall in the middle.

#include <cstdio>     const int maxn = 5;  char map[maxn][maxn];  int ans, n;    bool isok(int x, int y) {      for (int i = x + 1; i <= n && map[i][y] != 'X'; i++)          if(map[i][y] == '0')              return false;      for (int i = x - 1; i >= 1 && map[i][y] != 'X'; i--)          if(map[i][y] == '0')              return false;      for (int i = y; i <= n && map[x][i] != 'X'; i++)          if (map[x][i] == '0')              return false;      for (int i = y - 1; i >= 1 && map[x][i] != 'X'; i--)          if (map[x][i] == '0')              return false;      return true;  }    void dfs(int x, int y, int p) {      for (int i = 1; i <= n; i++)          for (int j = 1; j <= n; j++)              if (map[i][j] == '.' && isok(i, j)) {                  map[i][j] = '0';                  dfs(i, j, p + 1);                  map[i][j] = '.';              }      if (ans < p)          ans = p;  }    int main() {      while (scanf("%d", &n) && n) {          gets(map[0]);          for (int i = 1; i <= n; i++)              gets(map[i] + 1);          ans = 0;          dfs(1, 1, 0);          printf("%d\n", ans);      }      return 0;  }  #include <cstdio>const int maxn = 5;char map[maxn][maxn];int ans, n;bool isok(int x, int y) {for (int i = x + 1; i <= n && map[i][y] != 'X'; i++)if(map[i][y] == '0')return false;for (int i = x - 1; i >= 1 && map[i][y] != 'X'; i--)if(map[i][y] == '0')return false;for (int i = y; i <= n && map[x][i] != 'X'; i++)if (map[x][i] == '0')return false;for (int i = y - 1; i >= 1 && map[x][i] != 'X'; i--)if (map[x][i] == '0')return false;return true;}void dfs(int x, int y, int p) {for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)if (map[i][j] == '.' && isok(i, j)) {map[i][j] = '0';dfs(i, j, p + 1);map[i][j] = '.';}if (ans < p)ans = p;}int main() {while (scanf("%d", &n) && n) {gets(map[0]);for (int i = 1; i <= n; i++)gets(map[i] + 1);ans = 0;dfs(1, 1, 0);printf("%d\n", ans);}return 0;}

Conflict.

Queen N can only put one row, but this question is not good, so use the full picture to play the game, just go back to dfs, the question is up to 4*4, the range is very small.

When I first thought about putting a piece, I would mark other places that could not be placed, and then try again, later, it was found that if a point is marked as valid when it is marked repeatedly, it is checked instead. Because of the small amount of data, it does not take much time.

After that, you can use the accumulated tag to subtract the tag one by one...

Code:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.