Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[ACM-ICPC] Traversal

KuoE0
March 13, 2013

[ACM-ICPC] Traversal

KuoE0

March 13, 2013
Tweet

More Decks by KuoE0

Other Decks in Programming

Transcript

  1. Latest update: Mar 13, 2013 Attribution-ShareAlike 3.0 Unported (CC BY-SA

    3.0) http://creativecommons.org/licenses/by-sa/3.0/
  2. DFS & stack 1 2 3 4 5 6 1

    1 2 2 3 5 5 3 4 4
  3. DFS & stack 1 2 3 4 5 6 1

    1 2 2 3 5 5 3 4 4 6 6
  4. DFS & stack 1 2 3 4 5 6 1

    1 2 2 3 5 5 3 4 4 6 6
  5. DFS & stack 1 2 3 4 5 6 1

    1 2 2 3 5 5 3 4 6 6 4
  6. DFS & stack 1 2 3 4 5 6 1

    1 2 3 5 5 3 4 6 6 4 2
  7. DFS & stack 1 2 3 4 5 6 1

    2 3 5 5 3 4 6 6 4 2 1
  8. Source Code // There are n nodes. // g is

    an adjacent matrix. void DFS( int now ) { if ( visited[ now ] == true ) return; visited[ now ] = true; for ( int i = 0; i < n; ++i ) { if ( g[ now ][ i ] == true ) DFS( i ); } return; }
  9. Source Code // There are n nodes. // g is

    an adjacent matrix. void DFS() { stack< int > stk; stk.push( 0 ); while ( stk.empty() != true ) { int now = stk.top(); visited[ now ] = true; bool noleaf = true; for ( int i = 0; i < n; ++i ) if ( g[ now ][ i ] == true && visited[ i ] != true ) { stk.push( i ); noleaf = false; break; } if ( noleaf == true ) stk.pop(); } }
  10. BFS & queue 1 2 3 4 5 6 1

    1 2 3 3 4 4 5 5
  11. BFS & queue 1 2 3 4 5 6 1

    1 2 3 4 4 5 5 6 6
  12. BFS & queue 1 2 3 4 5 6 1

    1 2 3 4 5 5 6 6
  13. BFS & queue 1 2 3 4 5 6 1

    1 2 3 4 5 5 6 6
  14. Source Code // There are n nodes. // g is

    an adjacent matrix. void BFS() { queue< int > que; que.push( 0 ); while ( que.empty() != true ) { int now = que.front(); que.pop(); visited[ now ] = true; for ( int i = 0; i < n; ++i ) if ( g[ now ][ i ] == true ) que.push( i ); } }
  15. 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7 5

    8 9 10 7 6 7 9 9 8 10
  16. 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7 11

    5 8 9 10 7 6 7 9 9 8 10 11
  17. 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7 11

    12 5 8 9 10 7 6 7 9 9 8 10 11 12
  18. 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7 11

    12 13 5 8 9 10 7 6 7 9 9 8 10 11 12 13
  19. 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7 11

    12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14
  20. 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7 11

    12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15
  21. 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7 11

    12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16
  22. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  23. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  24. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  25. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  26. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  27. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  28. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  29. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  30. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  31. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  32. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  33. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  34. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  35. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  36. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  37. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  38. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  39. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  40. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  41. 1 1 ࿝૏૸໎ٶ (BFS) 2 3 4 5 6 7

    11 12 13 14 5 8 9 10 14 7 6 7 9 9 8 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  42. 352 - The Seasonal War Sample Input Sample Output 6

    100100 001010 000000 110000 111000 010100 8 01100101 01000001 00011000 00000010 11000011 10100010 10000001 01100000 Image number 1 contains 3 war eagles. Image number 2 contains 6 war eagles.
  43. Source Code #include <iostream> #include <cstdio> #include <cstring> using namespace

    std; int n; char map[ 30 ][ 30 ]; bool visited[ 30 ][ 30 ]; int step[ 8 ][ 2 ] = { { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, 1 }, { 0, -1 }, { -1, 1 }, { -1, 0 }, { -1, -1 } }; void DFS( int r, int c ); int main() { int t = 0; while ( scanf( “%d”, &n ) != EOF ) { int ret = 0; for ( int i = 0; i < n; ++i ) scanf( “%s”, map[ i ] ); memeset( visited, 0, sizeof( visited ) );
  44. Source Code for ( int i = 0; i <

    n; ++i ) for ( int j = 0; j < n; ++j ) if ( map[ i ][ j ] == ‘1’ && visited[ i ][ j ] != true ) { DFS( i, j ); ++ret; } printf( “Image number %d contains %d war eagles.\n”, ++t, ret ); } return 0; } void DFS( int r, int c ) { if ( visited[ r ][ c ] == true ) return; visited[ r ][ c ] = true; for ( int i = 0; i < 8; ++i ) { int r2 = r + step[ i ][ 0 ], c2 = c + step[ i ][ 1 ]; if ( r2 >= 0 && r2 < n && c2 >= 0 && c2 < n && map[ r2 ][ c2 ] == ‘1’ ) DFS( r2, c2 ); } }