zoukankan      html  css  js  c++  java
  • UVA225-Golygons(dfs)

    Problem UVA225-Golygons

    Accept:307  Submit:3646

    Time Limit: 3000 mSec

     Problem Description

     Imagine a country whose cities have all their streets laid out in a regular grid. Now suppose that a tourist with an obsession for geometry is planning expeditions to several such cities.
    Starting each expedition from the central cross-roads of a city, the intersection labelled (0,0), our mathematical visitor wants to set off north, south, east or west, travel one block, and view the sights at the intersection (0,1) after going north, (0,-1) after going south, (1,0) after going east or (-1,0) after going west. Feeling ever more enthused by the regularity of the city, our mathematician would like to walk a longer segment before stopping next, going two blocks. What’s more, our visitor doesn’t want to carry on in the same direction as before, nor wishes to double back, so will make a 90o turn either left or right. The next segment should be three blocks, again followed by a right-angle turn, then four, five, and so on with ever-increasing lengths until finally, at the end of the day, our weary traveller returns to the starting point, (0,0).
    The possibly self-intersecting figure described by these geometrical travels is called a golygon.
    Unfortunately, our traveller will making these visits in the height of summer when road works will disrupt the stark regularity of the cities’ grids. At some intersections there will be impassable obstructions. Luckily, however, the country’s limited budget means there will never be more than 50 road works blocking the streets of any particular city. In an attempt to gain accountability to its citizens, the city publishes the plans of road works in advance. Our mathematician has obtained a copy of these plans and will ensure that no golygonal trips get mired in molten tar.
    Write a program that constructs all possible golygons for a city.

     Input

    Since our tourist wants to visit several cities, the input file will begin with a line containing an integer specifying the number of cities to be visited.
    For each city there will follow a line containing a positive integer not greater than 20 indicating the length of the longest edge of the golygon. That will be the length of the last edge which returns the traveler to (0,0). Following this on a new line will be an integer from 0 to 50 inclusive which indicates how many intersections are blocked. Then there will be this many pairs of integers, one pair per line, each pair indicating the x and y coordinates of one blockage.

     Output

    For each city in the input, construct all possible golygons. Each golygon must be represented by a sequence of characters from the set {n,s,e,w} on a line of its own. Following the list of golygons should be a line indicating how many solutions were found. This line should be formatted as shown in the example output. A blank line should appear following the output for each city.
    Note: See on the right the diagram of the 1st City

     Sample Input

    2 8 2 -2 0 6 -2 8 2 2 1 -2 0
     

     Sample Ouput

    wsenenws

    Found 1 golygon(s).


    Found 0 golygon(s).

    题解:很明显的dfs搜索,剪枝也很粗暴,如果剩下的步数不足以回到原点就返回。由于坐标可能是负的,因此原点坐标要取成一个足够大的正值。

    坑点:题目中没有说一个城市只能参观一次,但是实际上只能参观一次,需要vis数组。

      1 #include <bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 const int o = 105,Max = 250;
      6 
      7 int n,ans;
      8 int gra[Max][Max];
      9 int dx[] = {1,0,0,-1};
     10 int dy[] = {0,1,-1,0};
     11 bool vis[Max][Max];
     12 char Direction[] = {'e','n','s','w'};
     13 int sta[30],num[] = {0,3};
     14 
     15 //dir为0代表水平方向,为1代表垂直方向
     16 
     17 void dfs(int x,int y,int pos,int dir){
     18     //printf("pos:%d x:%d y:%d
    ",pos,x,y);
     19     if(pos == n+1){
     20         if(x==o && y==o){
     21             ans++;
     22             printf("%c",Direction[sta[1]]);
     23             for(int i = 2;i <= n;i++){
     24                 printf("%c",Direction[sta[i]]);
     25             }
     26             printf("
    ");
     27         }
     28         return;
     29     }
     30 
     31     int step = 0;
     32     for(int i = pos;i <= n;i++) step += i;
     33     if(abs(x-o)+abs(y-o) > step) return;
     34 
     35     if(dir == -1){
     36         for(int i = 0;i < 4;i++){
     37             int xx = x+pos*dx[i],yy = y+pos*dy[i];
     38             if(vis[xx][yy]) continue;
     39             int k;
     40             if(1<=i && i<3){
     41                 for(k = min(y,yy);k <= max(y,yy);k++){
     42                     if(gra[xx][k]) break;
     43                 }
     44                 if(k == max(y,yy)+1){
     45                     sta[pos] = i;
     46                     vis[xx][yy] = true;
     47                     dfs(xx,yy,pos+1,1);
     48                     vis[xx][yy] = false;
     49                 }
     50             }
     51             else{
     52                 for(k = min(x,xx);k <= max(x,xx);k++){
     53                     if(gra[k][yy]) break;
     54                 }
     55                 if(k == max(x,xx)+1){
     56                     sta[pos] = i;
     57                     vis[xx][yy] = true;
     58                     dfs(xx,yy,pos+1,0);
     59                     vis[xx][yy] = false;
     60                 }
     61             }
     62         }
     63     }
     64     else{
     65         if(dir==0){
     66             for(int i = 1;i < 3;i++){
     67                 int xx = x+pos*dx[i],yy = y+pos*dy[i];
     68                 if(vis[xx][yy]) continue;
     69                 int k;
     70                 for(k = min(y,yy);k <= max(y,yy);k++){
     71                     if(gra[xx][k]) break;
     72                 }
     73                 if(k == max(y,yy)+1){
     74                     sta[pos] = i;
     75                     vis[xx][yy] = true;
     76                     dfs(xx,yy,pos+1,1);
     77                     vis[xx][yy] = false;
     78                 }
     79             }
     80         }
     81         else if(dir==1){
     82             for(int j = 0;j < 2;j++){
     83                 int i = num[j];
     84                 int xx = x+pos*dx[i],yy = y+pos*dy[i];
     85                 if(vis[xx][yy]) continue;
     86                 int k;
     87                 for(k = min(x,xx);k <= max(x,xx);k++){
     88                     if(gra[k][yy]) break;
     89                 }
     90                 if(k == max(x,xx)+1){
     91                     sta[pos] = i;
     92                     vis[xx][yy] = true;
     93                     dfs(xx,yy,pos+1,0);
     94                     vis[xx][yy] = false;
     95                 }
     96             }
     97         }
     98     }
     99 }
    100 
    101 int main()
    102 {
    103 #ifdef GEH
    104     freopen("helloworld.01.inp","r",stdin);
    105 #endif
    106     int iCase;
    107     scanf("%d",&iCase);
    108     while(iCase--){
    109         ans = 0;
    110         memset(vis,false,sizeof(vis));
    111         memset(gra,0,sizeof(gra));
    112         //vis[o][o] = true;
    113         int k,x,y;
    114         scanf("%d%d",&n,&k);
    115         for(int i = 0;i < k;i++){
    116             scanf("%d%d",&x,&y);
    117             if(abs(x)>o || abs(y)>o) continue;
    118             gra[o+x][o+y] = 1; 
    119         }
    120         dfs(o,o,1,-1);
    121         printf("Found %d golygon(s).
    
    ",ans);
    122     }
    123     return 0;
    124 }
  • 相关阅读:
    机器学习——模型评估与选择
    论文等级
    python简介
    记忆力
    PyQt 5控件
    PyQt5对话框
    PyQt 5事件和信号
    PyQt 5菜单和工具栏
    PyQt 5布局管理
    PyQt 5的基本功能
  • 原文地址:https://www.cnblogs.com/npugen/p/9571370.html
Copyright © 2011-2022 走看看