1118 - int b = get_key(to); a = get_parent(a); b =...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include<iostream> #include<map> #include<string> using namespace std; int num; int dim; int dot[1000]; map<string,int> table; string fro, to; map<string,int>::iterator iter = table.find(s); if(iter!= table.end()){ return iter->second; } table[s] = num; dot[num] = num; return num++; } int i,ret = 0; int pos = first.length(); for(i=0;i<pos;i++){ ret += first[i]!= second[i]; if(ret > 1) return 0; } return ret == 1; } int get_parent(int node){ int p = dot[node]; while(p != node){ node = p; p = dot[node]; } return p; } void combine(int a, int b){ int pa = get_parent(a); int pb = get_parent(b); if(pa == pb){ ; } else if(pa < pb){ dot[pb] = pa; } else { dot[pa] = pb; pa = pb; } dot[a] = pa; dot[b] = pa; } int travel(){ int a = get_key(fro);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int b = get_key(to); a = get_parent(a); b = get_parent(b); return a==b; } void get_end(string &amp;key){ key = &quot;&quot;; int i,v; for(i=0;i&lt;dim;i++){ cin&gt;&gt;v; key += 'A' + v; } } int main(){ string first,last; int value,i; int a,b; int tstcase = 0; cin&gt;&gt;dim; while(dim){ table.clear(); num = 0; get_end(fro); get_end(to); cin&gt;&gt;value; while(value != -1){ first = last = &quot;&quot;; first += 'A' + value; for(i=1;i&lt;dim;i++){ cin&gt;&gt;value; first += 'A' + value; } a = get_key(first); for(i=0;i&lt;dim;i++){ cin&gt;&gt;value; last += 'A' + value; } b = get_key(last); if(reachable(first,last)) combine(a,b); cin&gt;&gt;value; } cout&lt;&lt;&quot;Maze #&quot;&lt;&lt;++tstcase &lt;&lt;&quot; can&quot;; if(!travel()){ cout&lt;&lt;&quot;not&quot;; } cout&lt;&lt;&quot; be travelled&quot;&lt;&lt;endl; cin&gt;&gt;dim; } return 0; }...
View Full Document

Page1 / 2

1118 - int b = get_key(to); a = get_parent(a); b =...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online