我爬了多少14人?


13

用登山术语来说,“ 14er”是指海拔在14 000英尺或以上的任何山脉。但是,还有另一个区别。要使一个高峰算作14er,它的“地理突出度”也必须达到300英尺或更多。这意味着要从一个14er爬到另一个14er,必须首先下降至少 300英尺,然后才能再次上升。举这个例子。1号线计为14000英尺,每条线计为100英尺。

  /\__/\  
 /      \ 
/        \

现在,这两个峰的高程足以计数,但它们之间的高程降幅不足以算作两个单独的峰。因此,其中一个算作14er,另一个算作“部分峰”。这是一个示例,其中两个峰算作两个单独的14er:

   /\    /\   
  /  \  /  \  
 /    \/    \ 
/            \

两个14ers之间的下降也可能会出现部分高峰。这是最后一个山脉的略微修改版本:

   /\      /\   
  /  \/\  /  \  
 /      \/    \ 
/              \

该山脉也算作两个14人。

您必须编写一个程序或函数,该程序或函数采用一个代表山脉的ascii艺术表示法,并返回该范围内有14个峰。您可以采用最方便的格式输入,包括2D字符数组,带换行符的字符串或带其他定界符的字符串。您可以假定所有输入将仅包含字符/\_,并且每行的长度将相同(包括尾随空格)。您还可以假设山脉从左下角以a /或a 开头_

如果山峰的最后一段不在底线,则可以假定山峰仅在此之后减小,例如

  /
 /
/

算作单个14er。

您不必处理无效的山脉。

这是一些示例I / O:

         /\___/\_             
        /        \    /\      
       /          \  /  \     
   _/\/            \/    \    
  /                       \   
 /                         \  
/                           \_

2

                  /\    /\
         /\      /  \  /  
  /\    /  \    /    \/   
 /  \  /    \  /          
/    \/      \/           

4

       /\                 
 _/\__/  \                
/         \               

1

      /\                  
     /  \   /\            
    /    \_/  \           
   /           \          
  /             \         
 /               \        
/                 \       

1

              /\          
    /\_/\    /  \_        
   /     \  /     \     /\
  /       \/       \   /  
 /                  \_/   
/                         

3

那么起跑线算作14,000英尺?
R. Kap

@ R.Kap是的,我认为这是正确的,假设您说的是底线。
Alex A.

1
我认为您应该在某处提到_比同一条线上的斜线低100英尺的地方。至少这就是您的最后一个测试案例所建议的。
Martin Ender

3
规格似乎很薄...我们可以像一个扁平的泥炭/ / / _ \ \ \ 吗?另外,我认为输入中的最高点应始终算作一个峰值,但未明确指定。一个人可以从一个较低的峰开始,然后以不同的计数结束。
feersum '16

2
它会一直持续下去吗?每列最多有一个非空格字符吗?
Leaky Nun

Answers:


2

JavaScript(ES6),133个字节

s=>[...s].map((_,i)=>(c=s[i%h*w+i/h|0])=="/"?++a>2&&(p+=!d,d=a=3):c=="\\"&&--a<1&&(d=a=0),w=s.search`
`+1,h=-~s.length/w,a=3,d=p=1)|p

说明

由于没有明确说明规格,因此有两个假设:

  • 底线是14,000英尺标记(因此网格上的所有位置都足够高以算作一个峰值)。
  • 网格从第一个峰值开始(或上升)(因为按照先前的假设它已经至少高出14,000英尺)。
  • 仅在下降300英尺然后上升300英尺之后,一个单独的峰才计数。

遍历c每列的字符(具体地说,遍历每列直到找到一个字符)。当前海拔高度存储在中a。固定在的最小值0和最大值之间3。计算下一个峰所需的移动方向存储在dfalse=向上,true=向下)中。如果a达到3dfalse,则峰p数增加,d并设置为true(向下)。一旦a达到0d就是重新设置为false(最多)。

var solution =

s=>
  [...s].map((_,i)=>   // loop
    (c=s[i%h*w+i/h|0]) // c = current character (moves down columns)
    =="/"?             // if c is '/'
      ++a>2&&          // increment a, if a equals 3 and d is true:
        (p+=!d,d=a=3)  // increment p, set d to true, clamp a to 3
    :c=="\\"&&         // if c is '\':
      --a<1&&          // decrement a, if a equals 0:
        (d=a=0),       // set d to false, clamp a to 0
    
    // Initialisation (happens BEFORE the above code)
    w=s.search`\n`+1,  // w = grid width
    h=-~s.length/w,    // h = grid height
    a=3,               // a = current altitude (min = 0, max = 3)
    d=                 // d = required direction (false = up, true = down)
    p=1                // p = number of found peaks
  )|p                  // output the number of peaks

var testCases = [`
/\\
`,`
/\\          
  \\         
   \\    /\\  
    \\  /  \\ 
     \\/    \\
`,`
\\    /
 \\  / 
  \\/  
`,`
            /\\            
         /\\/  \\/\\         
      /\\/        \\/\\      
   /\\/              \\/\\   
/\\/                    \\/\\
`,`
  /\\__/\\
 /      \\
/        \\
`,`
   /\\    /\\   
  /  \\  /  \\  
 /    \\/    \\ 
/            \\
`,`
   /\\      /\\   
  /  \\/\\  /  \\  
 /      \\/    \\ 
/              \\
`,`
         /\\___/\\_             
        /        \\    /\\      
       /          \\  /  \\     
   _/\\/            \\/    \\    
  /                       \\   
 /                         \\  
/                           \\_
`,`
                  /\\    /\\
         /\\      /  \\  /  
  /\\    /  \\    /    \\/   
 /  \\  /    \\  /          
/    \\/      \\/           
`,`
       /\\                 
 _/\\__/  \\                
/         \\               
`,`
      /\\                  
     /  \\   /\\            
    /    \\_/  \\           
   /           \\          
  /             \\         
 /               \\        
/                 \\       
`,`
              /\\          
    /\\_/\\    /  \\_        
   /     \\  /     \\     /\\
  /       \\/       \\   /  
 /                  \\_/   
/                         
`];
result.textContent = testCases.map(c=>c+"\n"+solution(c.slice(1,-1))).join`\n\n`;
<textarea id="input" rows="6" cols="40"></textarea><br /><button onclick="result.textContent=solution(input.value)">Go</button><pre id="result"></pre>


2

C,174字节

a[99],c,p,j,M,m;main(i){for(i=j=1;c=getchar(),~c;i++)c<11?a[i]=j++,i=0:c&4?a[i]=j:0;for(m=j;c=a[i++];c>a[i-2]?M-m>1&&c-m>1?M=c,m=j,p++:M<c?M=m=c:M:m>c?m=c:0);printf("%d",p);}

输入中需要末尾换行符,否则为+4个字节。


1

JavaScript(ES6),154个字节

s=>s.split`\n`.map((s,i)=>s.replace(/\S/g,(c,j)=>{e[j]=i+(c!='\\');e[j+1]=i+(c>'/')}),e=[])&&e.map(n=>h-n+d?h-n-d*3?0:(c++,d=-d,h=n):h=n,h=e[0],c=d=1)|c>>1

其中\n代表文字换行符。取消高尔夫:

function climb(string) {
    var heights = [];
    // Split the array into lines so that we know the offset of each char
    var array = string.split("\n");
    // Calculate the height (actually depth) before and after each char
    for (var i = 0; i < array.length; i++) {
        for (var j = 0; j < string.length; j++) {
            switch (array[i][j]) {
            case '\':
                heights[j] = i;
                heights[j+1] = i + 1;
                break;
            case '_':
                heights[j] = i + 1;
                heights[j+1] = i + 1;
                break;
            case '/':
                heights[j] = i + 1;
                heights[j+1] = i;
                break;
        }
    }
    var count = 1;
    // Start by looking for an upward direction
    var direction = 1;
    var height = heights[0];
    for (var k = 1; k < heights.length; k++) {
        if (heights[i] == height - direction * 3) { // peak or trough
            direction *= -1;
            count++; // we're counting changes of direction = peaks * 2
            height = heights[i];
        } else if (heights[i] == height + direction) {
            // Track the current peak or trough to the tip or base
            height = heights[i];
        }
    }
    return count >> 1;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.