HooLee
If you believe, you can!
C++博客
首頁(yè)
新隨筆
新文章
聯(lián)系
管理
poj1021_2D-Nim
題意:對(duì)比連個(gè)圖是否相同。相同的條件是A圖中每個(gè)連通的區(qū)域都在B圖中有一塊相同的區(qū)域與之對(duì)應(yīng)。經(jīng)過(guò)旋轉(zhuǎn)后對(duì)應(yīng)也可以。
解題思路:
思路一:暴力式。遍歷并存儲(chǔ)A圖中所有聯(lián)通的區(qū)域,然后在B中逐個(gè)尋找與之對(duì)應(yīng)的圖形。
思路二:等價(jià)轉(zhuǎn)化式。比較兩個(gè)圖中對(duì)應(yīng)點(diǎn)的“連通度”是否相同。相同輸出YES。空白點(diǎn)的連通度為0,空白點(diǎn)的連通度為它所在行和列與之相連的非空白點(diǎn)的個(gè)數(shù)。
方式二非常巧妙,將二維問(wèn)題轉(zhuǎn)化為了一維問(wèn)題,大大簡(jiǎn)化了問(wè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)
閱讀(509)
評(píng)論(0)
編輯
收藏
引用
所屬分類(lèi):
Java基礎(chǔ)練習(xí)
只有注冊(cè)用戶(hù)
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開(kāi)源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
編輯距離
閏年判斷
正則表達(dá)式簡(jiǎn)單筆記
Excel格式地址轉(zhuǎn)換
一道模擬題——機(jī)器人行走距離計(jì)算
排列練習(xí)2
素?cái)?shù)篩法
排列組合練習(xí)
排列組合
poj1068Parencodings
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問(wèn)
Chat2DB
管理
Copyright ©2025 小鼠標(biāo) Powered by:
博客園
模板提供:
滬江博客
<
2012年8月
>
日
一
二
三
四
五
六
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
31
1
2
3
4
5
6
7
8
常用鏈接
我的隨筆
我的評(píng)論
我參與的隨筆
隨筆分類(lèi)
(111)
C語(yǔ)言(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é)長(zhǎng)
媛姐
媛姐csdn
最新評(píng)論
1.?re: 線段樹(shù)
是這個(gè)樣子的,所以在OJ有時(shí)候“卡住”了也不要太灰心,沒(méi)準(zhǔn)真的不是自己的原因呢。
加油,祝你好運(yùn)啦!
--小鼠標(biāo)
2.?re: 線段樹(shù)
對(duì)于編程競(jìng)賽來(lái)說(shuō),Java所需時(shí)間一般為C/C++的兩倍。合理的競(jìng)賽給Java的時(shí)間限制是給C/C++的兩倍。
--傷心的筆
3.?re: poj1273--網(wǎng)絡(luò)流
過(guò)來(lái)看看你。
--achiberx
4.?re: (轉(zhuǎn))ubuntu11.10無(wú)法啟動(dòng)無(wú)線網(wǎng)絡(luò)的解決方法
膜拜大神。。查了一個(gè)下午資料終于在這里解決了問(wèn)題。。神牛說(shuō)的區(qū)域賽難道是ACM區(qū)域賽。。?
--Hang
5.?re: 快速排序、線性時(shí)間選擇
博主,謝謝你的文章。你的方法可以很好的處理分區(qū)基準(zhǔn)在數(shù)組中重復(fù)的情況,書(shū)上的方法遇到這種輸入會(huì)堆棧溢出。書(shū)上給出了解釋但給的方法貌似不簡(jiǎn)潔。
--lsxqw2004
閱讀排行榜
1.?單調(diào)隊(duì)列(5497)
2.?Linux select()函數(shù)使用(3990)
3.?快速排序、線性時(shí)間選擇(3719)
4.?poj3468--絕對(duì)經(jīng)典的線段樹(shù)題(3642)
5.?優(yōu)先隊(duì)列--堆實(shí)現(xiàn)(3309)
色婷婷综合久久久久中文字幕
|
久久久久久久女国产乱让韩
|
91久久精一区二区三区大全
|
久久国产精品久久
|
国产福利电影一区二区三区久久老子无码午夜伦不
|
香蕉99久久国产综合精品宅男自
|
久久精品亚洲一区二区三区浴池
|
久久国产精品久久
|
午夜精品久久久久久影视riav
|
亚洲va中文字幕无码久久不卡
|
久久精品视频91
|
波多野结衣久久精品
|
久久91精品久久91综合
|
亚洲精品综合久久
|
国产精品一久久香蕉国产线看
|
久久精品亚洲男人的天堂
|
久久久久成人精品无码中文字幕
|
久久精品中文字幕第23页
|
久久久av波多野一区二区
|
久久大香萑太香蕉av
|
精品乱码久久久久久夜夜嗨
|
久久精品中文无码资源站
|
久久久国产亚洲精品
|
久久久久一本毛久久久
|
97久久天天综合色天天综合色hd
|
亚洲成av人片不卡无码久久
|
国产精品久久99
|
狠狠色婷婷久久一区二区
|
久久一区二区免费播放
|
99久久婷婷国产一区二区
|
成人综合伊人五月婷久久
|
国产成年无码久久久久毛片
|
亚洲国产另类久久久精品小说
|
无码人妻少妇久久中文字幕
|
久久精品女人天堂AV麻
|
中文字幕久久欲求不满
|
国产毛片久久久久久国产毛片
|
国产精品久久精品
|
久久青草国产精品一区
|
香港aa三级久久三级
|
激情五月综合综合久久69
|