1. 程式人生 > >洛谷1498 南蠻圖騰【遞歸】

洛谷1498 南蠻圖騰【遞歸】

頂上 map inf define pri 分治 \n alt 兩個

題目:https://www.luogu.org/problemnew/show/P1498

題意:

題意就是輸入一個n,輸出一個n大小的三角形,但是又沒說大小怎麽定義。【一臉懵逼】

技術分享圖片

看了題解才搞明白n=1時就是最頂上那個小三角形

n=2就是把小三角形向下復制兩個,n=3就是把n=2的向下復制兩個。

思路:

感覺自己好像遞歸分治這些搞得挺差的。

像這道題目 因為涉及到空格挺麻煩的,我們在一開始並不知道需要多少空格。

所以比較好的做法就是我們倒著存三角形,然後倒著輸出。

每次都把三角形往右復制一個,往下復制一個就可以了。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3
#include<map> 4 #include<set> 5 #include<iostream> 6 #include<cstring> 7 #include<algorithm> 8 #include<vector> 9 #include<cmath> 10 #include<queue> 11 12 #define inf 0x7f7f7f7f 13 using namespace std; 14 typedef long long LL; 15 typedef pair<int
, int> pr; 16 17 int n, height, width; 18 char c[3001][3001]; 19 20 void copyright() 21 { 22 for(int i = 1; i <= height; i++){ 23 for(int j = 1; j <= width; j++){ 24 c[i][j + width] = c[i][j]; 25 } 26 } 27 } 28 29 void copydown() 30 { 31 for(int i = 1; i <= height; i++){
32 for(int j = 1; j <= width; j++){ 33 c[i + height][width / 2 + j] = c[i][j]; 34 } 35 } 36 } 37 38 int main() 39 { 40 scanf("%d", &n); 41 for(int i = 0; i < 3001; i++){ 42 for(int j = 0; j < 3001; j++){ 43 c[i][j] = ; 44 } 45 } 46 c[1][1] = c[2][2] = \\; 47 c[1][2] = c[1][3] = _; 48 c[1][4] = c[2][3] = /; 49 height = 2; 50 width = 4; 51 for(int i = 2; i <= n; i++){ 52 copyright(); 53 copydown(); 54 height *= 2; 55 width *= 2; 56 } 57 58 for(int i = height; i >= 1; i--){ 59 for(int j = 1; j <= width; j++){ 60 if(c[i][j] == \\)printf("/"); 61 else if(c[i][j] == /)printf("\\"); 62 else printf("%c", c[i][j]); 63 } 64 printf("\n"); 65 } 66 67 68 return 0; 69 }

洛谷1498 南蠻圖騰【遞歸】