HooLee
If you believe, you can!
C++博客
首頁
新隨筆
新文章
聯系
管理
poj1021_2D-Nim
題意:對比連個圖是否相同。相同的條件是A圖中每個連通的區域都在B圖中有一塊相同的區域與之對應。經過旋轉后對應也可以。
解題思路:
思路一:暴力式。遍歷并存儲A圖中所有聯通的區域,然后在B中逐個尋找與之對應的圖形。
思路二:等價轉化式。比較兩個圖中對應點的“連通度”是否相同。相同輸出YES。空白點的連通度為0,空白點的連通度為它所在行和列與之相連的非空白點的個數。
方式二非常巧妙,將二維問題轉化為了一維問題,大大簡化了問題。
代碼
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
小鼠標
閱讀(505)
評論(0)
編輯
收藏
引用
所屬分類:
Java基礎練習
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
編輯距離
閏年判斷
正則表達式簡單筆記
Excel格式地址轉換
一道模擬題——機器人行走距離計算
排列練習2
素數篩法
排列組合練習
排列組合
poj1068Parencodings
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Copyright ©2025 小鼠標 Powered by:
博客園
模板提供:
滬江博客
<
2013年4月
>
日
一
二
三
四
五
六
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
8
9
10
11
常用鏈接
我的隨筆
我的評論
我參與的隨筆
隨筆分類
(111)
C語言(3)
DP(9)
Java筆記(1)
Java基礎練習(25)
安卓(1)
本科畢設(1)
博弈(1)
大數(7)
回溯(2)
排序(10)
暑期培訓周賽(3)
數據結構(7)
數論(1)
水題(8)
圖論(24)
網選訓練(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
陳鋼
大鵬
黨姐
焦林楓
汪濤
小白學長
媛姐
媛姐csdn
最新評論
1.?re: 線段樹
是這個樣子的,所以在OJ有時候“卡住”了也不要太灰心,沒準真的不是自己的原因呢。
加油,祝你好運啦!
--小鼠標
2.?re: 線段樹
對于編程競賽來說,Java所需時間一般為C/C++的兩倍。合理的競賽給Java的時間限制是給C/C++的兩倍。
--傷心的筆
3.?re: poj1273--網絡流
過來看看你。
--achiberx
4.?re: (轉)ubuntu11.10無法啟動無線網絡的解決方法
膜拜大神。。查了一個下午資料終于在這里解決了問題。。神牛說的區域賽難道是ACM區域賽。。?
--Hang
5.?re: 快速排序、線性時間選擇
博主,謝謝你的文章。你的方法可以很好的處理分區基準在數組中重復的情況,書上的方法遇到這種輸入會堆棧溢出。書上給出了解釋但給的方法貌似不簡潔。
--lsxqw2004
閱讀排行榜
1.?單調隊列(5493)
2.?Linux select()函數使用(3979)
3.?快速排序、線性時間選擇(3690)
4.?poj3468--絕對經典的線段樹題(3639)
5.?優先隊列--堆實現(3304)
看久久久久久a级毛片
|
久久婷婷国产剧情内射白浆
|
国产精品久久久久影视不卡
|
精品久久久久久国产
|
久久亚洲精品无码播放
|
久久久久久曰本AV免费免费
|
国产精品青草久久久久婷婷
|
一级a性色生活片久久无少妇一级婬片免费放
|
久久免费精品一区二区
|
人妻少妇精品久久
|
青青青国产成人久久111网站
|
久久热这里只有精品在线观看
|
久久91精品国产91久久户
|
精品久久久无码21p发布
|
999久久久国产精品
|
久久久久免费看成人影片
|
婷婷久久综合九色综合九七
|
色综合久久久久网
|
国产婷婷成人久久Av免费高清
|
久久久久久久免费视频
|
国产叼嘿久久精品久久
|
久久99精品国产
|
欧美噜噜久久久XXX
|
亚洲国产精品狼友中文久久久
|
成人精品一区二区久久
|
久久99精品综合国产首页
|
久久精品无码专区免费东京热
|
久久精品国产久精国产果冻传媒
|
亚洲午夜精品久久久久久浪潮
|
久久久久婷婷
|
97精品伊人久久大香线蕉
|
久久福利资源国产精品999
|
久久久黄色大片
|
狠狠色婷婷久久一区二区
|
久久久久久伊人高潮影院
|
欧美噜噜久久久XXX
|
欧洲人妻丰满av无码久久不卡
|
国产精品女同久久久久电影院
|
国内精品九九久久久精品
|
91精品国产高清久久久久久国产嫩草
|
伊人久久大香线蕉亚洲五月天
|