HooLee
If you believe, you can!
C++博客
首頁
新隨筆
新文章
聯(lián)系
管理
poj1021_2D-Nim
題意:對比連個(gè)圖是否相同。相同的條件是A圖中每個(gè)連通的區(qū)域都在B圖中有一塊相同的區(qū)域與之對應(yīng)。經(jīng)過旋轉(zhuǎn)后對應(yīng)也可以。
解題思路:
思路一:暴力式。遍歷并存儲(chǔ)A圖中所有聯(lián)通的區(qū)域,然后在B中逐個(gè)尋找與之對應(yīng)的圖形。
思路二:等價(jià)轉(zhuǎn)化式。比較兩個(gè)圖中對應(yīng)點(diǎn)的“連通度”是否相同。相同輸出YES??瞻c(diǎn)的連通度為0,空白點(diǎn)的連通度為它所在行和列與之相連的非空白點(diǎn)的個(gè)數(shù)。
方式二非常巧妙,將二維問題轉(zhuǎn)化為了一維問題,大大簡化了問題。
代碼
1
import
java.io.
*
;
2
import
java.util.
*
;
3
class
Main
4
{
5
private
static
int
[][] map1
=
new
int
[
110
][
110
];
6
private
static
int
[][] map2
=
new
int
[
110
][
110
];
7
private
static
CountNode[] ctnd1
=
new
CountNode[
110
*
110
];
8
private
static
CountNode[] ctnd2
=
new
CountNode[
110
*
110
];
9
private
static
int
col;
10
private
static
int
row;
11
public
static
void
main(String[] args)
12
{
13
14
Scanner sc
=
new
Scanner(System.in);
15
int
t
=
sc.nextInt();
16
for
(
int
ii
=
0
; ii
<
t; ii
++
)
17
{
18
for
(
int
j
=
0
; j
<
map1.length; j
++
)
19
{
20
Arrays.fill(map1[j],
0
);
21
Arrays.fill(map2[j],
0
);
22
}
23
Arrays.fill(ctnd1,
new
CountNode(
0
));
24
Arrays.fill(ctnd2,
new
CountNode(
0
));
25
26
col
=
sc.nextInt();
27
row
=
sc.nextInt();
28
int
n
=
sc.nextInt();
29
int
thiscol, thisrow;
30
//
31
//
System.out.println("col=" + col + " row=" + row + " n=" + n);
32
//
33
for
(
int
k
=
0
; k
<
n; k
++
)
//
read map1[][]
34
{
35
thiscol
=
sc.nextInt();
36
thisrow
=
sc.nextInt();
37
38
map1[thisrow][thiscol]
=
1
;
39
}
40
for
(
int
k
=
0
; k
<
n; k
++
)
//
read map2[][]
41
{
42
thiscol
=
sc.nextInt();
43
thisrow
=
sc.nextInt();
44
45
map2[thisrow][thiscol]
=
1
;
46
}
47
//
48
/**/
/*
System.out.println("map1[][]");
49
for(int i = 0; i < row; i++)
50
{
51
for(int j = 0; j < col; j++)
52
System.out.print(map1[i][j]);
53
System.out.println();
54
}
55
System.out.println("map2[][]");
56
for(int i = 0; i < row; i++)
57
{
58
for(int j = 0; j < col; j++)
59
System.out.print(map2[i][j]);
60
System.out.println();
61
}
*/
62
//
63
getAllCounts();
64
Arrays.sort(ctnd1);
65
Arrays.sort(ctnd2);
66
boolean
flag
=
true
;
67
for
(
int
i
=
0
; i
<
ctnd1.length; i
++
)
68
{
69
if
(ctnd1[i].getCount()
!=
ctnd2[i].getCount())
{
70
flag
=
false
;
71
System.out.println(
"
NO
"
);
72
break
;
73
}
74
}
75
if
(flag)
76
System.out.println(
"
YES
"
);
77
}
78
79
}
80
private
static
void
getAllCounts()
81
{
82
int
p
=
0
;
83
for
(
int
i
=
0
; i
<
row; i
++
)
84
{
85
for
(
int
j
=
0
; j
<
col; j
++
)
86
{
87
if
(map1[i][j]
==
1
)
88
ctnd1[p
++
]
=
new
CountNode(getThisMap1Counts(i, j));
89
if
(map2[i][j]
==
1
)
90
ctnd2[p
++
]
=
new
CountNode(getThisMap2Counts(i, j));
91
}
92
}
93
}
94
private
static
int
getThisMap1Counts(
int
r,
int
c)
//
map1[][]的連通度
95
{
96
int
count
=
0
;
97
for
(
int
i
=
r
-
1
; i
>=
0
&&
map1[i][c]
==
1
; i
--
)
98
count
++
;
99
for
(
int
i
=
r
+
1
; i
<
row
&&
map1[i][c]
==
1
; i
++
)
100
count
++
;
101
for
(
int
i
=
c
-
1
; i
>=
0
&&
map1[r][i]
==
1
; i
--
)
102
count
++
;
103
for
(
int
i
=
c
+
1
; i
<
col
&&
map1[r][i]
==
1
; i
++
)
104
count
++
;
105
return
count
+
1
;
106
}
107
private
static
int
getThisMap2Counts(
int
r,
int
c)
//
map2[][]的連通度
108
{
109
int
count
=
0
;
110
for
(
int
i
=
r
-
1
; i
>=
0
&&
map2[i][c]
==
1
; i
--
)
111
count
++
;
112
for
(
int
i
=
r
+
1
; i
<
row
&&
map2[i][c]
==
1
; i
++
)
113
count
++
;
114
for
(
int
i
=
c
-
1
; i
>=
0
&&
map2[r][i]
==
1
; i
--
)
115
count
++
;
116
for
(
int
i
=
c
+
1
; i
<
col
&&
map2[r][i]
==
1
; i
++
)
117
count
++
;
118
return
count
+
1
;
119
}
120
121
122
}
123
class
CountNode
implements
Comparable
<
CountNode
>
124
{
125
private
int
count;
126
public
CountNode(
int
count)
127
{
128
this
.count
=
count;
129
}
130
public
int
getCount()
131
{
132
return
count;
133
}
134
public
int
compareTo(CountNode n2)
135
{
136
return
n2.count
-
this
.count;
137
}
138
}
139
posted on 2013-04-14 20:10
小鼠標(biāo)
閱讀(510)
評論(0)
編輯
收藏
引用
所屬分類:
Java基礎(chǔ)練習(xí)
只有注冊用戶
登錄
后才能發(fā)表評論。
【推薦】100%開源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
編輯距離
閏年判斷
正則表達(dá)式簡單筆記
Excel格式地址轉(zhuǎn)換
一道模擬題——機(jī)器人行走距離計(jì)算
排列練習(xí)2
素?cái)?shù)篩法
排列組合練習(xí)
排列組合
poj1068Parencodings
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Copyright ©2025 小鼠標(biāo) Powered by:
博客園
模板提供:
滬江博客
<
2012年6月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
常用鏈接
我的隨筆
我的評論
我參與的隨筆
隨筆分類
(111)
C語言(3)
DP(9)
Java筆記(1)
Java基礎(chǔ)練習(xí)(25)
安卓(1)
本科畢設(shè)(1)
博弈(1)
大數(shù)(7)
回溯(2)
排序(10)
暑期培訓(xùn)周賽(3)
數(shù)據(jù)結(jié)構(gòu)(7)
數(shù)論(1)
水題(8)
圖論(24)
網(wǎng)選訓(xùn)練(8)
隨筆檔案
(127)
2014年3月 (1)
2013年7月 (10)
2013年5月 (1)
2013年4月 (11)
2013年3月 (8)
2012年10月 (1)
2012年9月 (12)
2012年8月 (38)
2012年7月 (14)
2012年6月 (2)
2012年5月 (8)
2012年4月 (6)
2012年3月 (6)
2012年2月 (4)
2011年8月 (5)
friends
陳鋼
大鵬
黨姐
焦林楓
汪濤
小白學(xué)長
媛姐
媛姐csdn
最新評論
1.?re: 線段樹
是這個(gè)樣子的,所以在OJ有時(shí)候“卡住”了也不要太灰心,沒準(zhǔn)真的不是自己的原因呢。
加油,祝你好運(yùn)啦!
--小鼠標(biāo)
2.?re: 線段樹
對于編程競賽來說,Java所需時(shí)間一般為C/C++的兩倍。合理的競賽給Java的時(shí)間限制是給C/C++的兩倍。
--傷心的筆
3.?re: poj1273--網(wǎng)絡(luò)流
過來看看你。
--achiberx
4.?re: (轉(zhuǎn))ubuntu11.10無法啟動(dòng)無線網(wǎng)絡(luò)的解決方法
膜拜大神。。查了一個(gè)下午資料終于在這里解決了問題。。神牛說的區(qū)域賽難道是ACM區(qū)域賽。。?
--Hang
5.?re: 快速排序、線性時(shí)間選擇
博主,謝謝你的文章。你的方法可以很好的處理分區(qū)基準(zhǔn)在數(shù)組中重復(fù)的情況,書上的方法遇到這種輸入會(huì)堆棧溢出。書上給出了解釋但給的方法貌似不簡潔。
--lsxqw2004
閱讀排行榜
1.?單調(diào)隊(duì)列(5499)
2.?Linux select()函數(shù)使用(3993)
3.?快速排序、線性時(shí)間選擇(3724)
4.?poj3468--絕對經(jīng)典的線段樹題(3646)
5.?優(yōu)先隊(duì)列--堆實(shí)現(xiàn)(3312)
久久激情亚洲精品无码?V
|
久久黄色视频
|
国产精品久久久久影院嫩草
|
成人资源影音先锋久久资源网
|
91久久精品电影
|
久久精品国产99久久久古代
|
久久精品www
|
久久亚洲AV无码精品色午夜麻豆
|
亚州日韩精品专区久久久
|
一本一道久久综合狠狠老
|
久久久久亚洲av毛片大
|
奇米影视7777久久精品
|
国内精品久久久久影院网站
|
亚洲伊人久久大香线蕉综合图片
|
久久久国产精品网站
|
国产A级毛片久久久精品毛片
|
国产三级精品久久
|
色偷偷久久一区二区三区
|
性做久久久久久久久久久
|
日本免费久久久久久久网站
|
亚洲国产精品无码久久久蜜芽
|
精品久久久久一区二区三区
|
久久久久成人精品无码中文字幕
|
久久免费香蕉视频
|
久久se这里只有精品
|
亚洲国产精品久久66
|
精品综合久久久久久97超人
|
综合网日日天干夜夜久久
|
色诱久久av
|
亚洲另类欧美综合久久图片区
|
精品久久国产一区二区三区香蕉
|
久久国产亚洲高清观看
|
无码人妻精品一区二区三区久久久
|
久久亚洲精品成人AV
|
久久久久久伊人高潮影院
|
久久精品国产亚洲AV久
|
日韩AV无码久久一区二区
|
伊人久久久AV老熟妇色
|
久久久久久综合网天天
|
色偷偷88888欧美精品久久久
|
色88久久久久高潮综合影院
|