http://newoj.acmclub.cn/problems/1999
1999: 三角形or四邊形?
題目描述:
JiangYu很無聊,所以他拿釘子在板子上戳出了一個由.#組成的10*10八聯通點陣圖。 請機智的你判斷圖中的#組成的是三角形還是四邊形?
其中一種3 jiao *為
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . # . . . . .
. . . # . # . . . .
. . # . . . # . . .
. # . . . . . # . .
######### .
. . . . . . . . . .
. . . . . . . . . .
其中一種4 bian *為
. . . . . . . . . .
. . . . . . . . . .
. ########.
. # . . . . . . #.
. # . . . . . . #.
. # . . . . . . #.
. ########.
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
輸入:
一個10*10點陣圖
輸出:
三角形輸出"3 jiao *“ 四邊形輸出"4 bian *”
..........
..........
..........
....#.....
...#.#....
..#...#...
.#.....#..
#########.
..........
..........
3 jiao *
題目描述:
JiangYu很無聊,所以他拿釘子在板子上戳出了一個由.#組成的10*10八聯通點陣圖。 請機智的你判斷圖中的#組成的是三角形還是四邊形?
其中一種3 jiao *為
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . # . . . . .
. . . # . # . . . .
. . # . . . # . . .
. # . . . . . # . .
######### .
. . . . . . . . . .
. . . . . . . . . .
其中一種4 bian *為
. . . . . . . . . .
. . . . . . . . . .
. ########.
. # . . . . . . #.
. # . . . . . . #.
. # . . . . . . #.
. ########.
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
輸入:
一個10*10點陣圖
輸出:
三角形輸出"3 jiao *“ 四邊形輸出"4 bian *”
【分析】:
斜四邊形:行2尖以及列2尖、 平四邊形:0尖、 梯形:1個行尖,0個列尖
正放三角形:1個行尖、倒放三角形:1個列尖 (但是另一個都不為0)
【BFS】:
【代碼】:

#include<iostream> #include<algorithm> #include<string.h> #include<cstring> #include<cstdio> using namespace std; #define ll long long #define ull unsigned long long #define mod 1000000007 #define inf 0x3f3f3f3f #define N 15 using namespace std; char s[N][N]; const int dx[]={-1,0,1,1,1,0,-1,-1}; const int dy[]={-1,-1,-1,0,1,1,1,0}; bool ok(int x, int y) //未越界+遇到# { return x>=0 && y>=0 && x<10 && y<10 && s[x][y]=='#'; } int dfs(int x, int y, int d) { int res = 0; s[x][y]='.'; int nx = x + dx[d],ny = y + dy[d]; if(!ok(nx,ny)) res++; else return res+=dfs(nx,ny,d); for(int i=0;i<8;i++) { int nx=x+dx[i],ny=y+dy[i]; if(ok(nx,ny)) return res+=dfs(nx,ny,i); } return res; } int main() { for(int i=0;i<10;i++) scanf("%s",s+i); int cnt=0; for(int i=0;i<10;i++){ for(int j=0;j<10;j++){ if(s[i][j]=='#') cnt = dfs(i,j,0); } } if(cnt>4) puts("4 bian *"); else puts("3 jiao *"); } /* 保證每條邊長度>2,保證所有的#之間是聯通的。 ..#....... .#.#...... #####..... .......... .......... .......... .......... .......... .......... .......... ....#..... ...##..... ..#.#..... ...##..... ....#..... .......... .......... .......... .......... .......... ...#...... ..#.#..... .#...#.... ..#.#..... ...#...... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .....###.. .....#.#.. .....###.. ....#..... ...##..... ..#.#..... .#..#..... .####..... .......... .......... .......... .......... .......... ....#..... ...##..... ..#.#..... .#..#..... #####..... .......... .......... .......... .......... .......... */

#include<iostream> #include<algorithm> #include<string.h> #include<cstring> #include<cstdio> using namespace std; #define ll long long #define ull unsigned long long #define mod 1000000007 #define inf 0x3f3f3f3f #define N 15 using namespace std; char s[N][N]; int main() { int f=0,f1=0,cnt,cnt1; for(int i=0;i<10;i++) gets(s[i]); for(int i=0;i<10;i++) { cnt=cnt1=0; for(int j=0;j<10;j++) { if(s[i][j]=='#') cnt++; if(s[j][i]=='#') cnt1++; } if(cnt == 1) { f++ ; } if(cnt1 == 1){ f1++; } } if(f==1&&f1!=0 || f1==1) puts("3 jiao *"); else puts("4 bian *"); return 0; } /* 保證每條邊長度>2,保證所有的#之間是聯通的。 ..#....... .#.#...... #####..... .......... .......... .......... .......... .......... .......... .......... ....#..... ...##..... ..#.#..... ...##..... ....#..... .......... .......... .......... .......... .......... ...#...... ..#.#..... .#...#.... ..#.#..... ...#...... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .....###.. .....#.#.. .....###.. ....#..... ...##..... ..#.#..... .#..#..... .####..... .......... .......... .......... .......... .......... ....#..... ...##..... ..#.#..... .#..#..... .####..... .......... .......... .......... .......... .......... */