码迷,mamicode.com
首页 > 其他好文 > 详细

UVa810

时间:2019-06-05 20:13:23      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:out   roo   space   flag   ||   ssi   iostream   print   ==   

这道题不想说什么了,如果不能想出更好的解决方法的话,打表是个好东西!

// UVa 810
#include <cstdio>
#include <iostream>
#include <cstring> 
#include <string> 
#include <vector> 
#include <set> 
using namespace std; 

const int maxn = 10 + 5; 

int pic[7][7]; 

void init() {
    pic[1][2] = 4; pic[1][3] = 2; pic[1][4] = 5; pic[1][5] = 3;
    pic[2][1] = 3; pic[2][3] = 6; pic[2][4] = 1; pic[2][6] = 4;
    pic[3][1] = 5; pic[3][2] = 1; pic[3][5] = 6; pic[3][6] = 2;
    pic[4][1] = 2; pic[4][2] = 6; pic[4][5] = 1; pic[4][6] = 5;
    pic[5][1] = 4; pic[5][3] = 1; pic[5][4] = 6; pic[5][6] = 3;
    pic[6][2] = 3; pic[6][3] = 5; pic[6][4] = 2; pic[6][5] = 4;
}

int n, m, a, b, t, f;
int G[maxn][maxn];

struct Node {
  int x, y, top, face, root;  
  Node(int x=0, int y=0, int top=0, int face=0, int root=0) : 
    x(x), y(y), top(top), face(face), root(root) {}
  bool operator < (Node b) const {
    if (x != b.x) return x < b.x;
    if (y != b.y) return y < b.y;
    if (top != b.top) return top < b.top;
    return face < b.face;  
  }
  bool isVis() const {
    return x >= 1 && x <= n && y >= 1 && y <= m && G[x][y] != 0;     
  }
};

int cnt;
set<Node> Set;
vector<Node> Ans; 

void findnode(Node u, int rem) {
  int x1, y1, t, f, l;
  x1 = u.x; y1 = u.y; t = u.top; f = u.face;      
  l = pic[t][f]; 
  Node a1(x1, y1-1, 7-l, f, rem);
  if (a1.isVis() && !Set.count(a1) && (G[x1][y1-1] == -1 || G[x1][y1-1] == t)) {
    Set.insert(a1);
    Ans.push_back(a1);
  }
  Node a2(x1, y1+1, l, f, rem); 
  if (a2.isVis() && !Set.count(a2) && (G[x1][y1+1] == -1 || G[x1][y1+1] == t)) {
    Set.insert(a2);
    Ans.push_back(a2);     
  }
  Node a3(x1-1, y1, f, 7-t, rem); 
  if (a3.isVis() && !Set.count(a3) && (G[x1-1][y1] == -1 || G[x1-1][y1] == t)) {
    Set.insert(a3);
    Ans.push_back(a3);     
  }
  Node a4(x1+1, y1, 7-f, t, rem);
  if (a4.isVis() && !Set.count(a4) && (G[x1+1][y1] == -1 || G[x1+1][y1] == t)) {
    Set.insert(a4);
    Ans.push_back(a4);    
  }
}

bool bfs() {    
  Node v(a, b, t, f, -1);
  Ans.push_back(v); 
  while (cnt < Ans.size()) {
    Node u = Ans[cnt++]; 
//    cout << u.x << " " << u.y << " " << u.top << " " << u.face << endl; 
    if (u.x == a && u.y == b && cnt-1) return true;  
    findnode(u, cnt-1);
  }
  return false; 
}

int count, flag; 

void DG(int num) {
  if (Ans[num].root != -1) DG(Ans[num].root);
  ++count;
  count %= 10; 
  if (flag) 
    printf(",");
  else { 
    printf("  ");
    flag = 1;
  }
  if (count == 0) {
    printf("\n");
    printf("  "); 
    count = 1; 
  }
  printf("(%d,%d)", Ans[num].x, Ans[num].y);     
}

int main() { 
  init();
  string str;
  while (cin >> str && str != "END") {
    cout << str << "\n"; 
    cin >> n >> m >> a >> b >> t >> f;  
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j) 
        cin >> G[i][j];
    cnt = 0;
    Set.clear(); 
    Ans.clear();
    if (bfs()) {
      count = flag = 0;
      DG(cnt-1);
      printf("\n"); 
    }
    else 
      printf("  No Solution Possible\n"); 
  }
  return 0;
}

 

UVa810

标签:out   roo   space   flag   ||   ssi   iostream   print   ==   

原文地址:https://www.cnblogs.com/yifeiWa/p/10981419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!