Uvs-225 golygons

Source: Internet
Author: User

Question: take steps 1, 2,... n from the origin point to the number of solutions to return to the origin point. No obstacles are allowed. You must turn around each time.

Train of Thought: a relatively simple DFS, the results have been done for a long time

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const int MAXN = 250;const int Add = 100;int n, ans;int G[MAXN][MAXN], step[MAXN], sum[MAXN];int dx[4]={1, 0, 0, -1};int dy[4]={0, 1, -1, 0};char sign[5]="ensw";bool check(int x, int y, int d, int k) {for (int i = 1; i <= k; i++) {x += dx[d];y += dy[d];if (abs(x) > Add || abs(y) > Add)continue;if (G[x+Add][y+Add] == -1)return true;}if (abs(x)+abs(y) > sum[20] - sum[k])return true;return false;}void dfs(int x, int y, int cnt, int last) {if (cnt > n) {if (x == 0 && y == 0) {for (int i = 1; i <= n; i++)printf("%c", sign[step[i]]);printf("\n");ans++;}return;}int &i = step[cnt];for (i = 0; i < 4; i++) {if (i == last || i+last == 3)continue;if (check(x, y, i, cnt))continue;int nx = x + dx[i]*cnt;int ny = y + dy[i]*cnt;if (G[nx+Add][ny+Add])continue;G[nx+Add][ny+Add] = 1;dfs(nx, ny, cnt+1, i);G[nx+Add][ny+Add] = 0;}}int main() {sum[0] = 0;for (int i = 1; i <= 20; i++)sum[i] = sum[i-1] + i;int t, k;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &k);memset(G, 0, sizeof(G));ans = 0;for (int i = 0; i < k; i++) {int a, b;scanf("%d%d", &a, &b);if (abs(a) > Add || abs(b) > Add)continue;G[a+Add][b+Add] = -1;}dfs(0, 0, 1, -1);printf("Found %d golygon(s).\n\n", ans);}return 0;}



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.