java的bfs_java写bfs

Find a way

InputThe input contains multiple test cases.

Each test case include, first two integers n, m. (2<=n,m<=200).

Next n lines, each line included m character.

‘Y’ express yifenfei initial position.

‘M’    express Merceki initial position.

‘#’ forbid road;

‘.’ Road.

‘@’ KCF

OutputFor each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.Sample Input

4 4

Y.#@

....

.#..

@..M

4 4

Y.#@

....

.#..

@#.M

5 5

Y..@.

.#...

.#...

@..M.

#...#

Sample Output

66

88

66

1 import java.util.ArrayDeque;

2 import java.util.PriorityQueue;

3 import java.util.Scanner;

4 class node{

5 int x;

6 int y;

7 int step;

8 int flag;

9 }

10 public class Main {

11 static int n,m;

12 static int [][][] vis = new int[220][220][2];

13 static char[][] c = new char[220][220];

14 static int [][][] dis = new int[220][220][2];

15 static String[] s = new String[220];

16 static int [][] dir = {{1,0},{0,1},{-1,0},{0,-1}};

17 static node head,tail;

18 static ArrayDeque q = new ArrayDeque();

19 static void bfs() {

20 while(!q.isEmpty()) {

21 head = q.poll();

22 //System.out.println(head.x+" "+head.y+" "+head.flag);

23 //System.out.println("lallaa"+vis[head.x][head.y][head.flag]);

24 for(int i=0;i<4;i++) {

25 int tx = head.x+dir[i][0];

26 int ty = head.y+dir[i][1];

27 int tstep = head.step + 1;

28 if(tx<0||tx>=n||ty<0||ty>=m||c[tx][ty]=='#'||vis[tx][ty][head.flag]==1)

29 continue;

30 if(c[tx][ty]=='@') {

31 if(head.flag==0) {

32 // System.out.println("haah");

33 dis[tx][ty][0] = Math.min(dis[tx][ty][0], tstep);

34 vis[tx][ty][0] = 1;

35 }

36 else if(head.flag==1) {

37 dis[tx][ty][1] = Math.min(dis[tx][ty][1], tstep);

38 vis[tx][ty][1] = 1;

39 }

40 }

41 vis[tx][ty][head.flag] = 1;

42 tail = new node();

43 tail.flag = head.flag;

44 tail.step = head.step + 1;

45 tail.x = tx;

46 tail.y = ty;

47 q.offer(tail);

48 }

49 }

50 }

51 public static void main(String[] args) {

52 Scanner cin = new Scanner(System.in);

53 while(cin.hasNext()) {

54 n = cin.nextInt();

55 m = cin.nextInt();

56 for(int i=0;i

57 for(int j=0;j

58 for(int k=0;k<2;k++) {

59 dis[i][j][k] = 1000000;

60 }

61 }

62 }

63 for(int i=0;i

64 for(int j=0;j

65 for(int k=0;k<2;k++) {

66 vis[i][j][k] = 0;

67 }

68 }

69 }

70 for(int i=0;i

71 s[i] = cin.next();

72 }

73 for(int i=0;i

74 for(int j=0;j

75 c[i][j] = s[i].charAt(j);

76 if(c[i][j] == 'Y') {

77 head = new node();

78 head.step = 0;

79 head.flag = 0;

80 head.x = i;

81 head.y = j;

82 q.offer(head);

83 vis[i][j][0] = 1;

84 //bfs();

85 }

86 if(c[i][j] == 'M') {

87 head = new node();

88 head.step = 0;

89 head.flag = 1;

90 head.x = i;

91 head.y = j;

92 q.offer(head);

93 vis[i][j][1] = 1;

94

95 }

96 }

97 }

98 bfs();

99 int ans = 1000000;

100 for(int i=0;i

101 for(int j=0;j

102 if(c[i][j] == '@') {

103 ans = Math.min(ans,dis[i][j][0]+dis[i][j][1]);

104 }

105 }

106 }

107 System.out.println(ans*11);

108 }

109 }

110 }

标签:head,java,tx,ty,int,bfs,flag,static

来源: https://www.cnblogs.com/1013star/p/10353589.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值